1

I'd like to substitute some text in my code by using regex. I use replace feature in VS2019.

Here is the regex I used for finding: absoluteText_(.*)\((.*), (.*))

Here is regex I used for replacing: absoluteText_$1($3)

For example I'd like to substitute this text "absoluteText_12(A, B, C)" and the expected outcome after replacing is done is "absoluteText_12(B, C). But when I use my regexes the result I get is "absoluteText_12(C)".

In the group number 2 I want to take every characters between an open bracket character and a comma (regarding to this part of the finding regex: ((.*), ). I don't understand why the character B is not taken with character C, they both should be considered as the group number 3 and thus be inserted to the result.

Is there any mistake in my regexes? Or what might be causing me getting these result?

  • `.*` is greedy. Use lazy dot, `absoluteText_(.*)\((.*?), (.*)\)` ([demo](https://regex101.com/r/1ZY1WA/1)). Or, re-write the pattern to be more specific at what it matches. – Wiktor Stribiżew Aug 12 '21 at 10:36

1 Answers1

0

Your current pattern is not phrased properly, and also is not using capture groups properly. I would use this version:

Find:    absoluteText_(\d+)\([^,]+,\s*(.*?)\)
Replace: absoluteText_$1($2)

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360