regex - m 和 rx 之间有什么区别吗?

标签 regex syntax raku

他们似乎是 used interchangeably in the documentation .有什么区别吗,即使是在意图上?

最佳答案

作为您链接到状态的文档,

m/abc/;         # a regex that is immediately matched against $_
rx/abc/;        # a Regex object
/abc/;          # a Regex object

[...] Example of difference between m/ / and / / operators:

my $match;
$_ = "abc";
$match = m/.+/; say $match; say $match.^name; # OUTPUT: «「abc」␤Match␤» 
$match =  /.+/; say $match; say $match.^name; # OUTPUT: «/.+/␤Regex␤»

所以/.../返回 Regex可以作为值传递的对象,并用于稍后和多次匹配,以及 m/.../返回 Match立即执行匹配的对象。当您打印 Match 时对象,你得到匹配的结果,当你打印 Regex对象,您将获得正则表达式的文本表示。使用 m/.../在 Perl 6 中,您可以访问隐式 Match对象,$/ :

Match results are stored in the $/ variable and are also returned from the match. The result is of type Match if the match was successful; otherwise it is Nil.


区别可与 Python 的 re.compile 相媲美对比 re.match / re.search ,并且在 Perl 5 中存在类似的区别,您可以使用 qr/.../ 存储和重用正则表达式。对比 m/...//.../用于直接匹配。正如@raiph 指出的那样,并非所有 m/.../ 都会出现和 /.../导致直接匹配。相反,Perl 5 precompiles literal (static) regexes即使没有明确要求它。 (据推测,Perl 6 也执行此优化。)

关于regex - m 和 rx 之间有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50112461/

相关文章:

javascript - 找到一种方法来查找与正则表达式不匹配的字符是 javascript

javascript - 用于匹配特定数组增量的 JS Regex 忽略字符串和单独增量

c++ - 为什么 "class"中有 "template <class x>"?

c++ - "lambda expressions"有什么好处?

arrays - 关于懒惰 [ RAKU ]

raku - 无法将 if 语句分配给变量

javascript - 如何增加字符串中的每个数字?

javascript - 从 Javascript 中的字符串获取版本号?

java - "X ? a : b"表示法是什么意思?

variable-assignment - 变量的声明和赋值 : silently dropping of assigned values