[Tutor] Regular Expressions - who's not working, them or me?!
Barnaby Scott
barnabydscott at yahoo.com
Thu Nov 20 03:42:08 EST 2003
I'm sure I know the answer to the question in the
subject - me!
However I am completely stuck and cannot understand
why some REs I wrote are not finding matches. They
only seem to work at the beginning of the string.
Help!
Here is my little test script:
import re
teststr = 'a bunch of text to look in'
afewres = ['.+', 'a', '[a-z]*', 'b', '.\Z', 'o{2}',
'.$']
print 'Looking in "%s"\n' % teststr
for r in afewres:
p = re.compile(r)
m = p.match(teststr)
if m:
print 'Match for "%s": ' % r, m.group(0)
else:
print 'No match for "%s"' % r
And here is my output:
>>>
Looking in "a bunch of text to look in"
Match for ".+": a bunch of text to look in
Match for "a": a
Match for "[a-z]*": a
No match for "b"
No match for ".\Z"
No match for "o{2}"
No match for ".$"
>>>
But I was expecting:
Match for ".+": a bunch of text to look in
Match for "a": a
Match for "[a-z]*": a
Match for "b": b
Match for ".\Z": n
Match for "o{2}": oo
Match for ".$": n
Can anyone see why I'm not getting what I expected?
Apologies if this exposes a lamentable lack of
understanding - I am trying to learn!
Thanks
__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/
More information about the Tutor
mailing list