Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove decimal point when not between two digits

Tags:

c#

regex

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 string

599.75, Tigers.

Then I would like it to come back as

599.75, Tigers

I was thinking a line like:

strNewString = RegEx.Replace(strOrigString, strRegEx, string.Empty);

Where strRegEx would match just the .'s to remove but I'm having a hard time figuring out how to match only the . and not the things surrounding it as well.

like image 659
HackWeight Avatar asked Jul 06 '11 16:07

HackWeight


2 Answers

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)
like image 106
Donut Avatar answered Nov 19 '22 01:11

Donut


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.

like image 6
Alan Moore Avatar answered Nov 19 '22 02:11

Alan Moore