简体   繁体   中英

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. 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\s*#/mi

   /issue\s*#/mi

So the OR suppose to return 7 matches, but it return nothing.

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. Also, /m is useless as neither ^ nor $ are used. /i makes ISSUE # match, too.

#!/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:

say 4 | 8;  # 12

| in a regex is called "alternation".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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