Odd behaviour from re

Tim Peters tim.one at home.com
Thu Jun 21 23:22:13 EDT 2001


[Magnus Lie Hetland]
> I'm looping through a string with match - thinking
> that if I specify a starting position beyond the end
> of the string, no match would be returned... But
> the darn thing still matches, and gives me both
> a start and an end at position len(string)... Is
> this intentional?

If your regexp can match an empty string, yes.  String slices beyond the end
of a string are defined to return an empty string in all contexts:

>>> s = "abc"
>>> s[3:100]
''
>>>

> ...
> but that's not how it is with the string-method find...

Not so, but of course string.find can (just like regexps!) find *only* an
empty string then:

>>> "abc".find("", 3)
3
>>>





More information about the Python-list mailing list