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.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 in 175
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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật