consider following scenario
input string = "WIPR.NS"
i have to replace this with “WIPR2.NS”
i am using following logic.
match pattern = "(.*)\.NS$" \\ any string that ends with .NS
replace pattern = "$12.NS"
In above case, since there is no group with index 12, i get result $12.NS
But what i want is “WIPR2.NS”.
If i don’t have digit 2 to replace, it works in all other cases but not working for 2.
How to resolve this case?
Thanks in advance,
Alok
Usually depends entirely on your regex engine (I’m not familiar with those that use
$1to represent a capture group, I’m more used to\1but you’d have the same problem with that).Some will provide a delimiter that you can use, like:
which clearly indicates that you want capture group 1 followed by the literal
2.NS.In fact, by looking at this page, it appears that’s exactly the way to do it (assuming .NET):
Also keep in mind that Jay provides an excellent answer for this specific use case that doesn’t require capture groups at all (by just replacing
.NSwith2.NS).You may want to look into that as a possibility – I’ll leave this answer here since:
X([A-Z])4([A-Z])withX${1}5${2}, where you have variable text on either side of the bit you wish to modify.