如果后面有什么东西,那么捕获模式的正则表达式是什么?否则,捕获第一次出现的模式。
实施例
编辑例如:
答案 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