【问题标题】:Regex to find strings containing at least two capitalized letters in Perl正则表达式在 Perl 中查找包含至少两个大写字母的字符串
【发布时间】:2013-02-14 05:59:54
【问题描述】:

我正在寻找一个匹配特定字符串的正则表达式,该字符串在 Perl 中至少有两个大写字母。

【问题讨论】:

    标签: regex string perl


    【解决方案1】:

    试试这个:

    /^.*[A-Z].*[A-Z].*$/

    【讨论】:

      【解决方案2】:

      我不知道你到底需要什么:

      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
      

      【讨论】:

        【解决方案3】:

        为什么只使用 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 个大写字母!

        【讨论】:

          猜你喜欢
          • 2012-06-05
          • 2022-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-28
          • 1970-01-01
          • 2022-01-02
          • 1970-01-01
          相关资源
          最近更新 更多