1

Using any stream editor, (or vim), is there a fast, efficient yet simple way to do a substitution but only inside an already matched pattern. for example :

Replace all parameter "foo" with "bar" but only for the function "func" :

test(foo, baz) func(foo) truc(foo) func(test, foo)

->

test(foo, baz) func(bar) truc(foo) func(test, bar)

would be done by first searching : /func\((.*)\)/

...And then, inside the captured group (or at least the full match), do s/foo/bar/g

NOTE : I've read using sed to replace two patterns within a larger pattern

But I'm looking for a simpler way, not necessarily using sed

hl037_
  • 111

1 Answers1

0

Using Notepad++ editor, it also works with SublimeText (at least V3.0):

  • Ctrl+H
  • Find what: \bfunc\([^)]*\K\bfoo\b
  • Replace with: bar
  • UNcheck Match case
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

\bfunc\(    # literally func(, word boundary to avoid abcfunc
[^)]*       # 0 or more not closing parenthese
\K          # forget all we have seen until this position
\bfoo\b     # foo surrounded with word boundaries

Result for given example:

test(foo, baz) func(bar) truc(foo) func(test, bar)
Toto
  • 17,839