Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: change [A-Z] to U, [a-z] to L with one regex?

by daxim (Curate)
on Mar 19, 2013 at 11:07 UTC ( #1024265=note: print w/replies, xml ) Need Help??

in reply to change [A-Z] to U, [a-z] to L with one regex?

There are more than just upper-case and lower-case letter characters. Decide what you want to do with those you haven't considered yet, and put them into the map.

use Unicode::UCD qw(charinfo); my %category_map = ( Lu => 'U', # letter, upper Ll => 'L', # letter, lower ); my $r = 'SomEThingWithMIXEDRegiSter'; $r =~ s { (.) # capture the character } { $category_map{ charinfo(ord $1)->{category} } }egmsx; # ULLUULLLLULLLUUUUUULLLULLL

Replies are listed 'Best First'.
Re^2: change [A-Z] to U, [a-z] to L with one regex?
by ikegami (Pope) on Mar 20, 2013 at 07:00 UTC

    Simpler solution not limited to non-ASCII chars:

    s/(\p{Lu})|\p{Ll}/ defined($1) ? 'U' : 'L' /eg;
Re^2: change [A-Z] to U, [a-z] to L with one regex?
by LanX (Archbishop) on Mar 20, 2013 at 01:20 UTC
    what about punctuation and numbers? =)

    my 2

    DB<141> $r = 'SomEThingWithMIXEDRegiSter;.?=356'; => "SomEThingWithMIXEDRegiSter;.?=356" DB<142> $r =~ s/(.)/ $1 eq "\u$1" ? ($1 eq "\l$1" ? $1 : "U" ) : ($1 + eq "\l$1" ? "L" : "X" ) /ge => 33 DB<143> $r => "ULLUULLLLULLLUUUUUULLLULLL;.?=356"

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      I'm pretty sure $1 eq "\u$1" can fail for some characters it shouldn't fail for.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1024265]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2020-02-25 19:01 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (113 votes). Check out past polls.