matching exactly a 4 digit number in python
skip at pobox.com
skip at pobox.com
Fri Nov 21 17:18:01 EST 2008
>> I am a few months new into python. I have used regexps before in perl
>> and java but am a little confused with this problem.
>> I want to parse a number of strings and extract only those that
>> contain a 4 digit number anywhere inside a string
>> However the regexp
>> p = re.compile(r'\d{4}')
>> Matches even sentences that have longer than 4 numbers inside strings
>> ..for example it matches "I have 3324234 and more"
Try this instead:
>>> pat = re.compile(r"(?<!\d)(\d{4})(?!\d)")>>> for s in x:
... m = pat.search(s)
... print repr(s),
... print (m is not None) and "matches" or "does not match"
...
' I have 2004 rupees ' matches
' I have 3324234 and more' does not match
' As 3233 ' matches
'2323423414 is good' does not match
'4444 dc sav 2412441 asdf ' matches
'random1341also and also' matches
'' does not match
'13' does not match
' a 1331 saves' matches
' and and as dad' does not match
' A has 13123123' does not match
'A 13123' does not match
'123 adn' does not match
'1312 times I have told you' matches
--
Skip Montanaro - skip at pobox.com - http://smontanaro.dyndns.org/
More information about the Python-list
mailing list