How to use re.search starting from a specific index in a row?

Sounds like a simple thing, but I don't see it. How to start a search in the middle of a line?

+4
source share
1 answer

The function re.searchdoes not accept an argument start, as methods do str. But searchthe compiled template re.compile/ method re.RegexObjecttakes an argument pos.

It makes sense if you think about it. If you really need to use the same regular expressions over and over again, you probably should compile them. Not so much for efficiency - the cache works well for most applications, but only for readability.


, , - ?

, . PCRE Google RE2 ICU, , , , , API.

regex, re stdlib ( , ) re, ( ) pos endpos search.


, , - " , ", : finditer search.

, str-:

i = 0
while True:
    i = s.find(sub, i)
    if i == -1:
        break
    do_stuff_with(s, i)

... :

for match in re.finditer(pattern, s):
    do_stuff_with(match)

, :

match = re.search(pattern, s[index:])

, , string , , 12GB mmap. (, 12GB mmap, , , ... , .)


, , index:

match = re.search('.{%d}%s' % (index, pattern), s)

, , , , .{20}, , , 20 , , . :

.{3}(abc)

Regular expression visualization

Debuggex

abcdefabcdef, 'abc' , abc.

, 'defabc'. , .{3} , match.group(1) .. , , match.group(0) . , .

+5

All Articles