Subscribe via Email

Subscribe via RSS/JSON


Recent Posts

Creative Commons Attribution 4.0 International License
© Rakhesh Sasidharan


Dot star replace matches twice

Have a look at this:

Works as expected. It matches “bc” in the input text and replaces the output with “ff” followed by whatever was matched (in this case “bc”).

The second example makes things clearer. It’s obvious in retrospect but easy to forget. We are not matching for the words “bc”, we are actually matching for a pattern “abc”: the text “bc” preceded by an “a”. If such a pattern is found, that matched pattern is replaced with the words “ff” followed by part of the matched pattern (in this case “bc”).

The third example simply drives home the point that the pattern can be anywhere in the input and that multiple instances are matched & replaced.

Another one:

What’s going wrong here? I expected the entire input to be replaced with one word – “blah”. Why do I have it twice then? This happens even if I do variations of the above thus:

It looks like the pattern matching-replacing is taking place twice. Lines 3 & 4 make it clearer above. The first time “abc” is replaced with “blah[abc]” as expected, and then a matching-replacing seems to occur again on that same input text adding “blah[]” to “blah[abc]”. No matching seems to take place however, or the matched pattern is empty, and that’s why there’s no text within the square brackets.

Hmm. So maybe that’s the problem! The matched pattern is empty – which is acceptable as my regexp pattern is .* and that matches empty text too. If I replace .* with .+ (at least one character must match) does that work as expected?

Sweet, it does!

Moral of the story: be careful using the .* pattern – apart from matching everything in the input, it will also match the emptiness that remains.

Dot star replace matches twice by rakhesh is licensed under a Creative Commons Attribution 4.0 International License.