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.12
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:
12345
67890
9876
I’ve tried using the following:
d+(?<!175)(b|$)
, which matched 3 empty strings,
text = "12345 67890 123175 9876"
matches = findall(r"d+(?<!175)(b|$)", text)
print(matches)
> ['', '', '']
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 in175
matches = findall(r"d+(?:175)", text)
> ['123175']
9
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 from revision 1 was close, but not correct since the Greedy quantifier (+
) would eat up all the digits, and then try to backtrack.
0
Demonstration of how a simple split and compare can be faster than using regular expressions:
import re
from timeit import timeit
import sys
import random
s = " ".join([str(random.randint(10_000, 1_000_000)) for _ in range(10_000)])
P1 = re.compile(r"d+")
def func2():
return [v for v in P1.findall(s) if not v.endswith("175")]
func2.__doc__ = f"re {P1.pattern} with string comparison"
P3 = re.compile(r"bd+b(?<!175)") # Wiktor Stribiżew
def func2a():
return P3.findall(s)
func2a.__doc__ = f"re {P3.pattern}"
def func3():
"""Split/compare (no re)"""
return [v for v in s.split() if not v.endswith("175")]
def func4():
"""Split/compare (no re) with integer check"""
return [v for v in s.split() if not v.endswith("175") and v.isdecimal()]
funclist = [func2, func2a, func3, func4]
if sys.version_info.major >= 3 and sys.version_info.minor >= 11:
P2 = re.compile(r"d++(?<!175)")
def func1():
return P2.findall(s)
func1.__doc__ = f"re {P2.pattern}"
funclist.append(func1)
if __name__ == "__main__":
for func in funclist:
duration = timeit(func, number=5_000)
print(func.__doc__, f"{duration:.4f}s")
if __debug__:
# assert that all functions return the same results
for f1, f2 in zip(funclist, funclist[1:]):
try:
assert f1() == f2()
except AssertionError:
print("Assertion failed", f1.__name__, f2.__name__)
else:
print("Not in debug mode. Assertions not tested")
Output:
re d+ with string comparison 4.9359s
re bd+b(?<!175) 4.7224s
Split/compare (no re) 1.9434s
Split/compare (no re) with integer check 2.7146s
re d++(?<!175) 3.9377s
Platform:
Python 3.13.1
MacOS 15.2
Apple M2
3
Your first attempt d+(?<!175)(b|$)
was almost right, just made the mistake of capturing the b|$
so findall
returned that instead of the matched number. You can fix it by not capturing there with d+(?<!175)(?:b|$)
, or simply with d+(?<!175)b
(since the $
is pointless there). Or the perhaps clearer d+b(?<!175)
.
4
Is it necessary to use regex?
content ='12345 67890 123175 9876'
for item in content.split(' '):
if not item.endswith('175'):
print(item)
Output
12345
67890
9876
1
An other alternative would be numeric validation:
Take the number modulo 1_000 and verify if this equals 175:
def numeric_validation(s:list)->list:
"""use modulo to verify numbers last digits"""
return [v for v in s.split() if not int(v)%1_000==175]
3