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*[\+\-])", ".")
<stuff><not a space followed by numbers>0<other numbers not followed by a plus or minus><more stuff>
" 100c" >>> " 100c"
"c00c10+" >>> "c..c10+"
" 0 0cc0c0-" >>> " 0 0cc.c0-"
You need to use a lookbehind, not a lookahead. Use
See the regex demo.
There is no need to use
\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.
(?<!\s\d*)- a negative lookbehind that fails the match if there is a whitespace and 0+ digits immediately to the left of the current location
0- a zero
(?!\d*[+-])- a negative lookahead that fails the match if there are 0+ digits followed with
+immediately to the right of the current location.