Reputation: 134
I want to turn any zero that is not preceded by a space and zero or arbitrary many numbers and not followed by zero or an arbitrary amount of numbers and a + or - into a dot.
So far, I've managed to replace every zero not followed by zero or an arbitrary amount of numbers and a + or - with a dot, using this:
Str1 = " 0 0c000c0+c0-c 10c0c"
Str2 = Regex.Replace(Str1, "(?!\s+\d*)0(?!\d*[\+\-])", ".")
This returns " . .c...c0+c0-c 1.c.c", so the last part is working, the first one isn't.
I want that zeros are replaced if the are following this pattern:
<stuff><not a space followed by numbers>0<other numbers not followed by a plus or minus><more stuff>
Examples:
" 100c" >>> " 100c"
"c00c10+" >>> "c..c10+"
" 0 0cc0c0-" >>> " 0 0cc.c0-"
How can I get this to work?
Upvotes: 3
Views: 96
Reputation: 626893
You need to use a lookbehind, not a lookahead. Use
(?<!\s\d*)0(?!\d*[+-])
^^^^^^^^^
See the regex demo.
There is no need to use +
after \s
in the lookbehind, as a single whitespace check is enough.
Note there is no need to escape +
in the character class and no need to escape -
if it is at the end/start of the character class.
Details
(?<!\s\d*)
- a negative lookbehind that fails the match if there is a whitespace and 0+ digits immediately to the left of the current location0
- a zero(?!\d*[+-])
- a negative lookahead that fails the match if there are 0+ digits followed with -
or +
immediately to the right of the current location.Upvotes: 3