Regular expression matching any subset of a given set?

后端 未结 4 399
面向向阳花
面向向阳花 2021-01-13 14:38

Is it possible to write a regular expression which will match any subset of a given set of characters
a1 ... an ?
I.e. it should match any string whe

4条回答
  •  花落未央
    2021-01-13 15:26

    Similar to Alan Moore's, using only \1, and doesn't refer to a capturing group before it has been seen:

    #!/usr/bin/perl
    my $re = qr/^(?:([abc])(?!.*\1))*$/;
    foreach (qw(ba pabc abac a cc cba abcd abbbbc), '') {
        print "'$_' ", ($_ =~ $re) ? "matches" : "does not match", " \$re \n";
    }
    

    We match any number of blocks (the outer (?:)), where each block must consist of "precisely one character from our preferred set, which is not followed by a string containing that character".

    If the string might contain newlines or other funny stuff, it might be necessary to play with some flags to make ^, $ and . behave as intended, but this all depends on the particular RE flavor.

    Just for sillyness, one can use a positive look-ahead assertion to effectively AND two regexps, so we can test for any permutation of abc by asserting that the above matches, followed by an ordinary check for 'is N characters long and consists of these characters':

    my $re2 = qr/^(?=$re)[abc]{3}$/;
    foreach (qw(ba pabc abac a cc abcd abbbbc abc acb bac bca cab cba), '') {
        print "'$_' ", ($_ =~ $re2) ? "matches" : "does not match", " \$re2 \n";
    }
    

提交回复
热议问题