python re.findall() with substring in alternations

前端 未结 1 1985
情话喂你
情话喂你 2021-01-27 11:29

If I have a substring (or \'subpattern\') of another string or pattern in a regex alternation, like so:

r\'abcd|bc\'

What is the expected behav

1条回答
  •  情歌与酒
    2021-01-27 11:54

    No backtracking takes place at all. Your pattern matches two different types of strings; | means or. Each pattern is tried out at each position.

    So when the expression finds abcd at the start of your input, that text matches your pattern just fine, it fits the abcd part of the (bc or abcd) pattern you gave it.

    Ordering of the alternative parts doesn't play here, as far as the regular expression engine is concerned, abcd|bc is the same thing as bc|abcd. abcd is not disregarded just because bc might match later on in the string.

    0 讨论(0)
提交回复
热议问题