[Tutor] Question regular expressions - the non-greedy pattern
Marcin Mleczko
Marcin.Mleczko at onet.eu
Mon Jan 21 15:45:05 CET 2013
Hello,
in the howto (http://docs.python.org/2/howto/regex.html#regex-howto)
there are code examples near the end of the page (the non-greedy
pattern) I am referring to:
s = '<html><head><title>Title</title>'
>>> len(s)
32
>>> print re.match('<.*>', s).span()
(0, 32)
>>> print re.match('<.*>', s).group()
<html><head><title>Title</title>
print re.match('<.*?>', s).group() #<- I'm referring to this
<html>
So far everything is fine.
Now I'm changing the input string to (adding an extra '<'):
s = '<<html><head><title>Title</title>'
and evoking the last command again:
print re.match('<.*?>', s).group()
I would expect to get the same result
<html>
as I'm using the non-greedy pattern. What I get is
<<html>
Did I get the concept of non-greedy wrong or is this really a bug?
I've treid this with
python -V
Python 2.7.3
on Win 7 64 Bit as well as Ubuntu 64 bit.
I'd be glad to here from you soon.
Thank's a lot for your effort.
best regards
Marcin
More information about the Tutor
mailing list