Regex: How do I replace part of a pattern and reference a variable within it?

后端 未结 3 553
借酒劲吻你
借酒劲吻你 2021-02-08 20:25

I want to match a pattern, replace part of the pattern, and use a variable within the pattern as part of the replacement string.

Is this correct?

/s/^((\\s

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-08 21:01

    This is called a backreference, and you use \i to refer to the i'th captured group from the pattern.

    So for the pattern ^((\s+)private\sfunction\s__construct\(\)), the replacement is \2def __init__.

提交回复
热议问题