Steve Yang Steve Yang - 6 days ago 6
Python Question

How to write python expression to filter out certain strings

There is a string consisting several numbers, for example:

"12.03 5.897 7.10.74 0.103 12.05 6.4.1 8.98"

I want to use Python regular expression to output only those numbers with ONLY ONE dot".", such as "12.03" and "5.897", not "7.10.74" and "6.4.1".

I know this is a trivial question without regex, I just want to solve this with regex. But I really couldn't figure out how to solve this with regex. Can somebody help me?

Answer

If you want a pure regex solution then use lookarounds:

>>> s = "12.03 5.897 7.10.74 0.103 12.05 6.4.1 8.98"
>>> print re.findall(r'(?<!\.)\b\d+\.\d+\b(?!\.)', s)
['12.03', '5.897', '0.103', '12.05', '8.98']

RegEx Demo

  • (?<!\.) is negative lookbehind to assert failure when previous char is DOT.
  • (?!\.) is negative lookahead to assert failure when next char is DOT.
  • \b is word boundary which is required on both sides to make sure we match full decimal number
Comments