Capture all numbers up to three digits

  • A+
Category:Languages

I have the following string:

1 2 134 2009 

And I'd like to capture the strings with between 1-3 digits, so the result should be:

['1', '2', '134'] 

What I have now captures those, but also captures the "first 3" digits in strings that contain more than 3 digits. This is the current regex I have:

>>> re.findall(r'/d{1,3}', '1 2 134 2009') ['1', '2', '134', '200', '9']  # or a bit closer --  >>> re.findall(r'/d{1,3}(?!/d)', '1 2 134 2009') ['1', '2', '134', '009'] 

What would be the correct way to make sure that another digit doesn't immediate proceed it?

 


Add word boundaries:

import re  result = re.findall(r'/b/d{1,3}/b', '1 2 134 2009')  print(result) 

Output

['1', '2', '134'] 

From the documentation /b:

Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of word characters. Note that formally, /b is defined as the boundary between a /w and a /W character (or vice versa), or between /w and the beginning/end of the string. This means that r'/bfoo/b' matches 'foo', 'foo.', '(foo)', 'bar foo baz' but not 'foobar' or 'foo3'.

By default Unicode alphanumerics are the ones used in Unicode patterns, but this can be changed by using the ASCII flag. Word boundaries are determined by the current locale if the LOCALE flag is used. Inside a character range, /b represents the backspace character, for compatibility with Python’s string literals.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: