Hukh
Hukh

Reputation: 63

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

Upvotes: 0

Views: 146

Answers (1)

choroba
choroba

Reputation: 242443

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".

Upvotes: 3

Related Questions