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

Re^2: creating a regular expression's alternation dynamically?

by integral (Hermit)
on Aug 06, 2004 at 14:39 UTC ( #380572=note: print w/replies, xml ) Need Help??


in reply to Re: creating a regular expression's alternation dynamically?
in thread creating a regular expression's alternation dynamically?

In the join, you might want to map quotemeta over @list so that any meta-chars (particulary |) get escaped.

Also you have to be careful that none of the strings are prefixes of another string, otherwise you'll find that ordering is significant in an alternation.

--
integral, resident of freenode's #perl

Replies are listed 'Best First'.
Re^3: creating a regular expression's alternation dynamically?
by mifflin (Curate) on Aug 06, 2004 at 17:27 UTC
    yep, good point, thanks!
    use strict; use warnings; # create alternation my @list; while (<DATA>) { chomp; push @list, $_; } my $regex = '^(' . join('|', map {quotemeta} @list) . ')$'; my $s = 'huey'; print "regex = $regex\n"; print "match:\t$1\n" if $s =~ m/$regex/; __DATA__ huey de|wey louie
    output is...
    # perl test.pl regex = ^(huey|de\|wey|louie)$ match: huey

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://380572]
help
Chatterbox?
[Corion]: I hope all is well marto! ;) My godson had a surprise visit to the hospital yesterday because he fell and had cut his skin besides his eye, but everything was glued together again and all is well
[marto]: good grief, that's not fun, glad to hear all is as well as could be :)
[Corion]: marto: Yeah - their mother picked all three of them up at the kindergarden to then go to the hospital, and all three of them were well behaved, and all also were quite obedient when they came home, so they recognized the situation
[Corion]: Nice to see that the kids have some understanding of important things that happen and the appropriate behaviour :)

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2016-12-08 09:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (137 votes). Check out past polls.