You can use a negative forecast ahead to add an exception:
\b(?!914)\d{3}\b
The word boundary \b ensures that we match the number as a whole word.
Watch the regex demo and IDEONE demo :
import re p = re.compile(r'\b(?!914)\d{3}\b') test_str = "123\n235\n456\n1000\n910 911 912 913\n 914\n915 916" print(re.findall(p, test_str))
Output:
['123', '235', '456', '910', '911', '912', '913', '915', '916']
source share