http://www.perlmonks.org?node_id=767749

gibsonca has asked for the wisdom of the Perl Monks concerning the following question:

How do I detect one or more uppercase characters in a string? Been trying things like this:

if ($str =~ m/[A-Z]/) { print "str contains an uc char\n" }

Replies are listed 'Best First'.
Re: Simple Regex Question
by kennethk (Abbot) on Jun 02, 2009 at 19:49 UTC
    Your posted code accomplishes the posted spec. Perhaps you trimmed a little too much out of your snippet?
Re: Simple Regex Question
by almut (Canon) on Jun 02, 2009 at 22:16 UTC

    Strictly speaking, there are more uppercase letters in this world than A-Z.  So with Unicode in mind, you might want to use

    my $str = "foo Ä bar"; print "matched\n" if $str =~ /\p{Lu}/;

    \p{Lu} stands for "property letter uppercase" — see perlunicode.

Re: Simple Regex Question
by jrsimmon (Hermit) on Jun 02, 2009 at 19:44 UTC
    $str = "abcdefghijklmnopQrstuzwxyz"; if($str =~ /[A-Z]/){print "match!";}
Re: Simple Regex Question
by Lawliet (Curate) on Jun 02, 2009 at 21:41 UTC

    Your code only matches one uppercase letter. /[A-Z]+/ will match one or more.

    However, now that I reread your question, it seems you only care if there is at least one uppercase letter which, as stated by other posters, your code accomplishes.

    I don't mind occasionally having to reinvent a wheel; I don't even mind using someone's reinvented wheel occasionally. But it helps a lot if it is symmetric, contains no fewer than ten sides, and has the axle centered. I do tire of trapezoidal wheels with offset axles. --Joseph Newcomer