Get all possible matches for regex (in python)?

前端 未结 3 1097
难免孤独
难免孤独 2020-12-18 16:17

I have a regex that can match a string in multiple overlapping possible ways. However, it seems to only capture one possible match in the string, how can I get all possible

3条回答
  •  佛祖请我去吃肉
    2020-12-18 16:31

    If you want to detect overlapping matches, you'll have to implement it yourself - essentially, for a string foo

    1. Find the first match that starts at string index i
    2. Run the matching function again against foo[i+1:]
    3. Repeat steps 1 and 2 on the incrementally short remaining portion of the string.

    It gets trickier if you're using arbitrary-length capture groups (e.g. (.*)) because you probably don't want both foo-foobar and oo-foobar as matches, so you'd have to do some extra analysis to move i even farther than just +1 each match; you'd need to move it the entire length of the first captured group's value, plus one.

提交回复
热议问题