C # regex - remove decimal point if not between two digits

I clear the search bar and should delete any periods that appear, but retain decimal points when they are between two digits.

For example, if I have a string

599.75, tigers.

Then I would like him to come back as

599.75, tigers

I was thinking of a line like:

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

Where strRegEx will match only. for deletion, but it's hard for me to determine how to match only. not the things around him.

+6
source share
3 answers

You must use the lookahead and lookbehind statements . They actually do not match the characters at your input, but only determine if a match is possible. You can use negative images and negative images to do the opposite of this, which is appropriate. Using the following for strRegEx will correspond to periods that are not surrounded by numbers:

 (?<!\d)\.(?!\d) 
+7
source

As I read the question, you want to combine the point only if it is not preceded and not followed by numbers. For example, in the following list, you would like to combine a point on each line except the last, because it is the only one that has numbers on either side of it.

  abc.
 .def
 xy
 123.
 .456
 x.78
 90.x
 599.75 

The accepted answer (?<!\d)\.(?!\d) matches only in the first three lines; this is 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 it comes to harmonizing the text, especially when it comes to using calls.

+2
source

You can use something like \.(?!\d)

0
source

Source: https://habr.com/ru/post/892155/


All Articles