Search code examples
regexnotepad++

Extend string between strings


startABCend
->
startABC123end

I seek to capture text between start and end, and extend it, as shown. I tried:

  • find = start.*end, replace = \1 123: will capture start and end and between, but replace them all
  • find = (?s)(?<=start).+?(?=end), replace = \1 123: will keep start and end but replace captured

How to accomplish this with regex in N++?


The exact use case is

func_name(a, b=1) -> func_name(a, b=1, c=2)
# can also be
func_name(g=5, k=7) -> func_name(g=5, k=7, c=2)
# so capture between `func_name(` and `)` and extend with `, c=2`

Solution

  • Your example target does not include the white space you have in your replace string. To accomplish using the group AND append numbers you can use brackets.

    Basically:

    Find: (?<=start)(.+?)(?=end)
    Replace: (\1)123
    
    or just
    
    Find: start(.+?)end
    Replace: start(\1)123end