Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Getting characters from regex pattern

by devnul (Monk)
on Jun 25, 2008 at 02:19 UTC ( [id://693863]=perlquestion: print w/replies, xml ) Need Help??

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

Is there an "easy way" (more on that elusive term in a moment) to get this:
perl -e 'my $r = q/Capri|Classic/; print $1 if "capri" =~ m/($r)/i;'
to return the characters from the regex (mixed/upper- lowercase) -- I.E. "Capri", not "capri"? Or to populate it in some variable (it can go into $1, if thats doable)...

Preferrably in a cleaner way then doing an eval inside the regex with some kind of map, or something. And not having to build a separate hash that maps the lower-case to mixed-case version.. In other words, something in the regex internals, that might get set? Or something that might not take someone 20 minutes to figure out how it works at a later date?

Thanks (as always)!

- dEvNuL

Replies are listed 'Best First'.
Re: Getting characters from regex pattern
by NetWallah (Canon) on Jun 25, 2008 at 03:57 UTC
    How about this (dispatch subroutines):
    my $r = q/Capri|Classic/; my $d= {capri => sub {print $_[0]}, somethingelse => sub { print 'handler for something else'} }; $r=~m/($_)/i and $d->{$_}->($1) for keys %$d ; #-- prints "Capri"

         Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax

Re: Getting characters from regex pattern
by ikegami (Patriarch) on Jun 25, 2008 at 04:05 UTC
    Maybe you don't actually need regular expressions?
    my @lists = ( [qw( Capri Class )], # ... ); OUTER: for my $list (@lists) { for my $word (@$list) { next if index($s, $word) < 0; print("$word\n"); next OUTER; # or last OUTER; } }
Re: Getting characters from regex pattern
by kyle (Abbot) on Jun 25, 2008 at 02:32 UTC

    It sounds to me as if you already have several solutions (eval, map, separate hash), but you don't want to use them for they are messy or complicated. My suggestion is to take one of them and encapsulate it off somewhere with some documentation that that tells future generations what it does.

    my $dollar_one = node_693863( 'capri', [ 'Capri', 'Classic' ] ); print $dollar_one if defined $dollar_one; # Documentation here sub node_693863 { # your implementation here }
Re: Getting characters from regex pattern
by graff (Chancellor) on Jun 25, 2008 at 06:51 UTC
    If your actual goal is to normalize case so that you get Initial Caps, you probably want to use ucfirst and lc:
    perl -le '@a=qw/capri cApRi TITLE-case/; for(@a){s/(\pL+)/ucfirst(lc $ +1)/ge; print}'
    prints:
    Capri Capri Title-Case
    (The "\pL+" in the regex matches all "letter" characters; if your data tends to involve content in languages with non-Latin, non-cased letters, you'll probably want to use "\p{LC}" or "\p{CasedLetter}+" instead.)
Re: Getting characters from regex pattern
by starbolin (Hermit) on Jun 25, 2008 at 06:51 UTC

    This does the transformation you asked for but I'm not sure it meets your readability requirement. Certainly not the way I'd parse tokens out of a string.

    $string="glarch capri foobar"; $r=q/Capri|Classic/; $string=~m/($r)/i; $r=~m/($1)/i; print $1; ... Returns 'Capri'


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
Re: Getting characters from regex pattern
by Anonymous Monk on Jun 25, 2008 at 13:36 UTC
    How about just reversing the match?

    perl -wMstrict -e "for (@ARGV) { if ('Capri,Classic,Dart' =~ m{ (\Q$_\E) }xmsi) { print qq($_ is a $1 \n) } else { print qq($_ isn't a car \n) } } " capri cLaSsIc foo capri is a Capri cLaSsIc is a Classic foo isn't a car
Re: Getting characters from regex pattern
by devnul (Monk) on Jun 25, 2008 at 22:39 UTC
    Great replies, everyone, thank you so much.... Gives me a few different ways of thinking about this problem, which I greatly appreciate...

    Thanks!

    - dEvnuL

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found