繁体   English   中英

为什么这个OR运算符无法在Perl中工作

[英]Why this OR operator not working in perl

我试图在perl中使用Binary OR运算符从文件中获取一些信息。 我用这个正则表达式

 /(fix|issue)\s*#/mi

但找不到任何匹配项。 在文件中,“ fix#”有4个匹配项,“ issue#”有3个匹配项,而在不使用OR运算符的情况下分别使用它们时,我的理解是正确的。

   /fix\s*#/mi

   /issue\s*#/mi

因此,OR假定返回7个匹配项,但不返回任何内容。

这两行完全在文件中

Add test for the example in issue #142.

 Fix #1190

我说的对吗?

感谢帮助

这个对我有用。 由于未提供/g因此每行匹配一次。 另外, /m无效,因为既不使用^也不使用$ /i使ISSUE #匹配。

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

while (<DATA>) {
    say "Matched $1" if /(fix|issue)\s*#/mi;
}

__DATA__
fix #
fix # fix #
---
issue #
issue # issue #

请注意,“ binary or”是在正则表达式之外使用的不同运算符:

say 4 | 8;  # 12

| 正则表达式中的“替代”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM