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?
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 :
(
)
in vi\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/g
if you want to replace all occurrenceshttp://vimregex.com should help you get started.
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__
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With