perl6 Is using junctions in matching possible?

前端 未结 1 1506
我寻月下人不归
我寻月下人不归 2021-01-12 17:25

Is it possible to use junction to match any of the values in a junction? I want to match any of the values in an array. What is the proper way to do it?

lisp         


        
相关标签:
1条回答
  • 2021-01-12 17:44

    Junctions are not meant to be interpolated into regexes. They're meant to be used in normal Perl 6 expressions, particularly with comparison operators (such as eq):

    my @a = <x y z>;
    say    "y" eq any(@a);  # any(False, True, False)
    say so "y" eq any(@a);  # True
    

    To match any of the values of an array in a regex, simply write the name of the array variable (starting with @) in the regex. By default, this is interpreted as an | alternation ("longest match"), but you can also specify it to be a || alternation ("first match"):

    my @a = <foo bar barkeep>;
    say "barkeeper" ~~ / @a /;     # 「barkeep」
    say "barkeeper" ~~ / || @a /;  # 「bar」
    
    0 讨论(0)
提交回复
热议问题