【发布时间】:2013-02-14 05:59:54
【问题描述】:
我正在寻找一个匹配特定字符串的正则表达式,该字符串在 Perl 中至少有两个大写字母。
【问题讨论】:
我正在寻找一个匹配特定字符串的正则表达式,该字符串在 Perl 中至少有两个大写字母。
【问题讨论】:
试试这个:
/^.*[A-Z].*[A-Z].*$/
【讨论】:
我不知道你到底需要什么:
perl -lane 'for(@F){if(/[A-Z]/){$count++ for m/[A-Z]/g}if($count >=2){print $_};$count=0}'
以下测试
> echo "ABC DEf Ghi" | perl -lane 'for(@F){if(/[A-Z]/){$count++ for m/[A-Z]/g}if($count >=2){print $_};$count=0}'
ABC
DEf
【讨论】:
为什么只使用 ASCII 字母?
这将匹配使用 Unicode character properties 的任何语言的两个大写字母。
/\p{Lu}.*\p{Lu}/
\p{Lu} 是一个 Unicode character property,它匹配具有小写变体的大写字母
另请参阅perlretut: More on characters, strings, and character classes
一个小测试:
my @input = ("foobar", "Foobar", "FooBar", "FÖobar", "fÖobÁr");
foreach my $item (@input) {
if ($item =~ /\p{Lu}.*\p{Lu}/) {
print $item . " has at least 2 uppercase!\n"
} else {
print $item . " has less than 2 uppercase!\n"
}
}
输出:
foobar 的大写字母少于 2 个!
Foobar 的大写字母少于 2 个!
FooBar 至少有 2 个大写字母!
FÖobar 至少有 2 个大写字母!
fÖobÁr 至少有 2 个大写字母!
【讨论】: