簡體   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