Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Building Regex Alternations Dynamically

by haukex (Archbishop)
on Jan 18, 2017 at 12:57 UTC ( [id://1179840]=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    my @values = qw/ a ab. d ef def g|h /;
    my ($regex) = map { qr/$_/ }          # 5.
    ...
        sort { length $b <=> length $a }  # 2.
        @values;                          # 1.
    print "$regex\n";                     # 6.
    
  2. or download this
    (?^:ab\.|def|g\|h|ef|a|d)
    
  3. or download this
    if ($input=~$regex) {
        print "It matches!\n";
    ...
    while ($input=~/($regex)/g) {
        print "Found string: $1\n";
    }
    
  4. or download this
    my %map = ( a=>1, ab=>23, cd=>45 );   # 1.
    my ($regex) = map { qr/$_/ }          # 2.
    ...
        s/($regex)/$map{$1}/g;            # 6.
        print "$before -> $_\n";          # 7.
    }
    
  5. or download this
    abcd -> 2345
    aacd -> 1145
    abaab -> 23123
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://1179840]
Front-paged by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-04-19 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found