I\'m cleaning up a search string and need to remove any periods that appear but keep decimal points when they are between two digits.
For example if I have a strin
You could use something like \.(?!\d)
The way I read the question, you want to match a dot only if it's not both preceded and followed by digits. For example, in the following list you would want to match the dot in every string except the last one, because that's the only one that has digits on both sides of it.
abc. .def x.y 123. .456 x.78 90.x 599.75
The accepted answer, (?<!\d)\.(?!\d)
, matches only in the first three strings; it's equivalent to:
a dot, ( (not preceded by a digit) AND (not followed by a digit) )
If my interpretation is correct, you want something like this:
(?<!\d)\.|\.(?!\d)
...which is equivalent to:
(a dot, not preceded by a digit) OR (a dot, not followed by a digit)
In any case, it pays to be as precise as you can when talking about text matching, especially when it involves the use of lookarounds.
You should take advantage of the lookahead and lookbehind assertions. They don't actually match characters in your input, but only determine whether a match is possible or not.
You can use negative lookaheads and negative lookbehinds to do the opposite of this, which is what's appropriate here. Using the following for strRegEx
will match periods that are not surrounded by digits:
(?<!\d)\.(?!\d)