9

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+)private\sfunction\s__construct\(\))/(2)def\s__init__

In English: Replace any amount of whitespace followed by the string "private function __construct()" with the same amount of whitespace and the string def __init__. So, is my regex bad or what?

partial replace

1
  • 1
    What language/application are you using this in? (Not the language your data is in. The one that is executing the expression.)
    – Brigand
    Jan 18, 2012 at 1:40

3 Answers 3

14

I presume you want to replace it in vi

Replace all occurrences

:s/^\(\s\+\)private function __construct()/\1def __init__/g

Replace first

:s/^\(\s\+\)private function __construct()/\1def __init__/

Few suggestions to your pattern

  • / is used in vi for search , use :
  • you need to escape ( ) in vi
  • use \i where i is xth capture group like \1 \2 to back reference grouped patterns in replacement
  • \s can not be used in replacement text use ' ' instead
  • use trailing /g if you want to replace all occurrences

http://vimregex.com should help you get started.

2
  • But here's an issue.... If I have {$\n\r replaceme, I want to search for {$\n\r\s+replaceme^, and I only want to replace \s+replaceme. The reason I want to do this is because there are times when " replaceme" isn't preceded by {$\n\r, and I don't want the command to activate on those. So, can I replace " replaceme" only when it's preceded by {$\n\r, and start my replacement with the same amount of whitespace? Jan 18, 2012 at 3:01
  • Many thanks for the link to vimregex.com. What a great page! :)
    – sjakobi
    Jan 25, 2020 at 3:44
6

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__.

2
  • Okay, but here's another example: :s/^((\s+)$this->).*$/\2self./. As you can see, I'm searching for ^(\s+)this->.*$, right? But I only want to replace this part of the search pattern: \s+this->. So, I used two sets of brackets.... Should I reference \2 or \1? Jan 18, 2012 at 3:04
  • The capturing brackets are counted left-to-right, so \1 for that example. Since this is in vi, you have to escape bits of the regular expression, see @PrashantBhate's answer. Either that or I use the \v flag in vim to use normal regex syntax, but I'm not sure if vi has it. Jan 18, 2012 at 3:21
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.