简体   繁体   English

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

[英]Why this OR operator not working in perl

I am trying to use Binary OR operator in perl to get some information from a file. 我试图在perl中使用Binary OR运算符从文件中获取一些信息。 I used this regular expression 我用这个正则表达式

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

but it does not find any match. 但找不到任何匹配项。 in the file there are 4 matches for 'fix #' and 3 match for 'issue #', and I got that right when using them separately without OR operator. 在文件中,“ fix#”有4个匹配项,“ issue#”有3个匹配项,而在不使用OR运算符的情况下分别使用它们时,我的理解是正确的。

   /fix\s*#/mi

   /issue\s*#/mi

So the OR suppose to return 7 matches, but it return nothing. 因此,OR假定返回7个匹配项,但不返回任何内容。

These two lines are exactly in the file 这两行完全在文件中

Add test for the example in issue #142.

 Fix #1190

Am I doing that correct? 我说的对吗?

Thanks for help 感谢帮助

It works for me. 这个对我有用。 It matches once per line as the /g wasn't given. 由于未提供/g因此每行匹配一次。 Also, /m is useless as neither ^ nor $ are used. 另外, /m无效,因为既不使用^也不使用$ /i makes ISSUE # match, too. /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 #

Note that "binary or" is a different operator used outside of regular expressions: 请注意,“ binary or”是在正则表达式之外使用的不同运算符:

say 4 | 8;  # 12

| in a regex is called "alternation". 正则表达式中的“替代”。

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

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