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

后端 未结 3 556
借酒劲吻你
借酒劲吻你 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条回答
  •  天涯浪人
    2021-02-08 20:57

    I don't think anyone really understood the question. Basically, the way I'm doing this is as follows:

    "If you want to search for a replacement pattern, pattern a, and replace it with a replacement string, pattern i, only if it starts with a pattern, pattern b, then you need to include pattern b in the replacement string, like this: :/(pattern b)(pattern a)/(pattern b)(i)/g".

    It's a little wordy but worth reading.

    In the past, I'm sure that someone has thought, "It could save a lot of resources to not actually replace pattern b with pattern b. It's redundant to do so." Maybe it happens automatically. I haven't found a built-in method in vi or any other program to do that. I'm sure I could write a script to do it, though.

提交回复
热议问题