New submission from Poul-Henning Kamp:
I'm surprised that this does not find any matches:
import re
r = re.compile(^abc)
s = 0123abcxyz
for i in range(0,len(s)):
print(i, r.search(s, i))
I would have expected the i==4 case to match ?
(This is on:
Python 2.7.3 (default,
Ned Deily added the comment:
Note the warning about '^' in the documentation for the re search method:
The optional second parameter pos gives an index in the string where the
search is to start; it defaults to 0. This is not completely equivalent to
slicing the string; the '^' pattern
Ned Deily added the comment:
To expand a bit, rather than multiple calls to search, you can use the start
and end methods of the match object to determine where the string (without the
'^' anchor) matches. For example:
r = re.compile(abc)
s = 0123abcxyz
match = r.search(s)
if match:
Poul-Henning Kamp added the comment:
I have tried hard, but have utterly failed to figure out why you have chosen
the semantics for ^ you mention, tried to come up with a plausible use case,
and I have utterly failed.
I find it distinctly counter intuitive.
I think the Principle of Least
Matthew Barnett added the comment:
The semantics of '^' are common to many different regex implementations,
including those of Perl and C#.
The 'pos' argument merely gives the starting position the search (C# also lets
you provide a starting position, and behaves in exactly the same way).