Regular expression: Lookahead and lookbehind, checking a. (period) to use as decimal and full stop

I had a good look at the network to answer this question, but it doesn't seem to work.

I developed the following regular expression:

(?<![^\d][\\])[\.](?![\d]) 

The goal is to identify any ".". (dots) that have not been escaped or are part of a decimal number.

t)

  • abc.co.uk, both points must match
  • ab0.co.uk, both points must match
  • abc.0.uk, both points must match
  • abc \ .co.uk, only the second dot maps
  • 0.00, point must not match
  • abc0.0.uk, the first point will not match (which is an acceptable result), the second point should

Currently it works for all of the above cases, except:

  • abc.0.uk, both points must match

Any thoughts? It seems that the appearance is working correctly, but you should not look.

I am sure that it will be easy for any Regex guru!

FYI. I am developing this in .net 4

+6
source share
2 answers

Try this one

 (?<![\\\d])\.(?=\d)|(?<=[^\D\\])\.(?!\d)|(?<=[^\d\\])\.(?!\d) 

See here at Regexr

I broke it in three steps.

  • Match, if you had not been an escape symbol before, not a digit, but a digit in the back.

  • Compare, if before it was not a symbol of evacuation, and the figure and behind is not a figure

  • Coincidence, if before it was not a symbol of evacuation, and not a number, and there is no number at the back

+6
source

Divide the task by 2. First check the string by the number or not [^.\d] . On the second, select from the points left without an exit symbol. This approach is much simpler.

Regex from another answer does not apply to all cases.

0
source

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


All Articles