I have a file where i have several recurrence of this
div.1
div.2
div.3
and so on
I want to replace with div1
, div2
, ...
In my case both pattern and replacement are regular expression This is the code I wrote
newstr = Regex.Replace(strFile, "div\.\d","div\d")
But it is replacing as following:
div.1 div\d
div.2 div\d
and so on
Any suggestions?
Regex is finding the pattern in the whole file but it is replacing it incorrectly as if replacement is considered as text and not regex.
I have tried in replacement value : $1 , div\d+$ both same result
Try:
(?<=div)\.(?=\d)
and replace with empty string.
See: regex101
Your issue is that you match the whole string and replace it with the same literal string div\d
. This way you only match the literal .
and keep your number in place as you "do not touch" it.
Explanation:
(?<=div)
: ensures literal div
before\.
: the literal dot is matched and also(?=\d)
: a number must follow