Finding # prefixing numbers
Caleb Hattingh
caleb1 at telkomsa.net
Tue Jul 19 17:26:13 EDT 2005
You really owe it to yourself to try the PyParsing package, if you have to
do this kind of thing with any frequency.
The syntactic difference between PyParsing and regular expressions is
greater than the syntactic difference between Python and C.
thx
Caleb
On Tue, 19 Jul 2005 13:35:02 +0200, peterbe at gmail.com <peterbe at gmail.com>
wrote:
> 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