Finding # prefixing numbers

peterbe at gmail.com peterbe at gmail.com
Tue Jul 19 13:35:02 CEST 2005


In a text that contains references to numbers like this: #583 I want to
find them with a regular expression but I'm having problems with the
hash. Hopefully this code explains where I'm stuck:

>>> import re
>>> re.compile(r'\b(\d\d\d)\b').findall('#123 x (#234) or:#456 #6789')
['123', '234', '456']
>>> re.compile(r'\b(X\d\d\d)\b').findall('X123 x (X234) or:X456 X6789')
['X123', 'X234', 'X456']
>>> re.compile(r'\b(#\d\d\d)\b').findall('#123 x (#234) or:#456 #6789')
[]
>>> re.compile(r'\b(\#\d\d\d)\b').findall('#123 x (#234) or:#456 #6789')
[]

As you can guess, I'm trying to find a hash followed by 3 digits word
bounded. As in the example above, it wouldn't have been a problem if
the prefix was an 'X' but that's not the case here.




More information about the Python-list mailing list