Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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" }

Comment on Simple Regex Question
Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2015-09-03 03:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred temperature scale is:










    Results (94 votes), past polls