Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Simple Regex Question

by gibsonca (Beadle)
on Jun 02, 2009 at 19:39 UTC ( #767749=perlquestion: print w/replies, xml ) Need Help??

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://767749]
Approved by ikegami
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2019-08-22 00:55 GMT
Find Nodes?
    Voting Booth?

    No recent polls found