I'm currently running a simple find-and-replace, on strings like this:
1. User.Name "John"
2. User.Age 20
3. Name.Length 5
However, trying to replace Name
with WHATEVER
results in this:
1. User.WHATEVER "John"
2. User.Age 20
3. WHATEVER.Length 5
I needed to change line 3, but not line 1. How do I check if the current word is after a dot ( .
) and skip replacing that word?
I'm in .NET 4.0 and my regex currently looks like this:
result = new Regex(@"\b" + oldWord + @"\b").Replace(text, newWord);
You can use a negative lookbehind on .
: (?<!\\.)
That gives:
result = new Regex(@"\b(?<!\.)" + oldWord + @"\b").Replace(text, newWord);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.