Get all possible matches for regex (in python)?

前端 未结 3 1098
难免孤独
难免孤独 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:42

    It's not something regex engines tend to be able to do. I don't know if Python can. Perl can using the following:

    local our @matches;
    "foo-foobar-foobaz" =~ /
        ^(.*)-(.*)\z
        (?{ push @matches, [ $1, $2 ] })
        (*FAIL)
    /xs;
    

    This specific problem can probably be solved using the regex engine in many languages using the following technique:

    my @matches;
    while ("foo-foobar-foobaz" =~ /(?=-(.*)\z)/gsp) {
       push @matches, [ ${^PREMATCH}, $1 ];
    }
    

    (${^PREMATCH} refers to what comes before where the regex matched, and $1 refers to what the first () matched.)

    But you can easily solve this specific problem outside the regex engine:

    my @parts = split(/-/, "foo-foobar-foobaz");
    my @matches;
    for (1..$#parts) {
       push @matches, [
          join('-', @parts[0..$_-1]),
          join('-', @parts[$_..$#parts]),
       ];
    }
    

    Sorry for using Perl syntax, but should be able to get the idea. Translations to Python welcome.

提交回复
热议问题