Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex to match a whole number not ending in some digits

I’ve not been able to construct a pattern which can return an whole numbers that don’t end in a sequence of digits. The numbers could be of any length, even single digits, but they will always be whole. Additionally, multiple numbers could be on the same line of text and I want to match them all. The numbers are always followed by either a single space or the end of the line or the end of the text. I’m matching in python 3.10

For example, over the text '12345 67890 123175 9876', let’s say I want to get all numbers not ending in 175.

I would want the following matches:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

12345
67890
9876

I’ve tried using the following:

  • \d+(?<!175)(\b|$), which matched 4 empty strings,
text = "12345 67890 123175 9876"
matches = findall(r"\d+(?!175)(\b|$)", text)
print(matches)
> ['', '', '', '']
  • \d+(?<!175), which matched all 4 numbers
matches = findall(r"\d+(?<!175)", text)
> ['12345', '67890', '12317', '9876']
  • \d+(?:175), which matched only the number ending in 175
matches = findall(r"\d+(?:175)", text)
> ['123175']

>Solution :

You can use is a negative lookbehind .*(?<!a) that ensures the string does not end with a.

\d++(?<!175)

Test here.

Note that Possessive Quantifier (++) has been introduced in Python 3.11. Your 2nd approach was close, but not correct since the Greedy quantifier (+) would eat up all the digits, and then try to backtrack.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading