Perl6从字符串中获取正则表达式

Ric*_*rth 8 regex perl6

我想把一个正则表达式放在一个YAML配置文件中(例如:config/filename.yaml),例如.

-
  section: Begining
  regex: '/ ^ <[ a..b, A..B ]> /'
-
  section: Next
  regex: '/ ^ <[ c..z, C..Z ]> /'
Run Code Online (Sandbox Code Playgroud)

当我把它读成哈希(例如,使用YAMLish)时,例如,

use YAMLish;
my @h = load-yaml('config/filename.yaml'.IO.slurp);
Run Code Online (Sandbox Code Playgroud)

我自然有一根绳子 @h[0]<regex>

那么如何从字符串中恢复正则表达式以便在匹配中使用?

我想要类似下面的东西,但以下不起作用:

say 'Its a beginning' if 'A beginning' ~~ @h[0]<regex>
Run Code Online (Sandbox Code Playgroud)

它不能按预期工作,因为它@h[0]<regex>是一个Str,因此智能匹配是@h[0]<regex>针对Str文字测试Str.那么如何让正则表达式脱离Str?

Chr*_*oph 11

您将变量中保存的字符串$var插入到正则表达式中/ <$var> /,或者,如果是更复杂的表达式,则将其插入到正则表达式中/ <{ @h[0]<regex> }> /.

但请注意,首先必须从字符串中删除封闭的斜杠.对于可信赖的输入,当然总是EVAL......