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

Re^2: Sparing multiple 'or's

by AnomalousMonk (Archbishop)
on Jun 04, 2018 at 16:28 UTC ( [id://1215858]=note: print w/replies, xml ) Need Help??


in reply to Re: Sparing multiple 'or's
in thread Sparing multiple 'or's

Note that the results for the examples of the two methods differ:

c:\@Work\Perl\monks>perl -wMstrict -le "my @cases = qw/ABA SCO ACC PHC GHF/; my $re = join '|', @cases; ;; for my $text ('SCO', 'ENDOSCOPE', 'Microsoft') { print qq{'$text' found (grep)} if grep { $text eq $_ } @cases; print qq{'$text' found (regex)} if $text =~ /^$re$/; } " 'SCO' found (grep) 'SCO' found (regex) 'ENDOSCOPE' found (regex)
See haukex's Building Regex Alternations Dynamically.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: Sparing multiple 'or's
by hippo (Bishop) on Jun 04, 2018 at 21:02 UTC

    Good catch (++). As dave_the_m suggested we can use brackets in this case:

    #!/usr/bin/env perl use strict; use warnings; use utf8; my @cases = qw/ABA SCO ACC PHC GHF/; my $re = join '|', @cases; for my $text ('SCO', 'ENDOSCOPE', 'Microsoft') { print "$text found (grep)\n" if grep {$text eq $_} @cases; print "$text found (regex)\n" if $text =~ /^($re)$/; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-24 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found