正则表达式:匹配以后出现后跟字符或更早返回?

时间:2012-04-26 13:38:30

标签: php regex pattern-matching

如果后面有什么东西,那么捕获模式的正则表达式是什么?否则,捕获第一次出现的模式。

实施例

  • 查找'FL':CA和FL(较小)的状态沿着海岸。
  • 查找'CA':CA和FL的状态沿着海岸。

编辑例如:

  • 查找FL后跟'(较小)'
  • 查找CA,因为FL后面没有'(较小)'

1 个答案:

答案 0 :(得分:1)

这对我来说并不完全清楚,但在这里尝试perl味道:

script.pl的内容:

use warnings;
use strict;

while ( <DATA> ) { 
    chomp;
    if ( m/
            (?(?=.*\(smaller\))                 # Positive look-ahead conditional expression.
                \b([[:upper:]]+)\s+\(smaller\)  # If succeed, match previous word only in uppercase.
                    |                           # Or
                \b([[:upper:]]+)\b)             # If failed, match first word in uppercase found.
        /x ) {    
        printf qq[%s -> %s\n], $_, $1 || $2;    # $1 has first conditional, $2 the second one.
    }   
}

__DATA__
The states of CA and FL (smaller) are along coasts.
The states of CA and FL are along coasts.

像以下一样运行:

perl script.pl

使用以下输出:

The states of CA and FL (smaller) are along coasts. -> FL
The states of CA and FL are along coasts. -> CA

UPDATED 与单行(输出相同):

perl -lne '
    printf qq[%s -> %s\n], $_, $1 || $2 
        if m/(?(?=.*\(smaller\))\b([[:upper:]]+)\s+\(smaller\)|\b([[:upper:]]+)\b)/
' infile