Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: how can i split a string to an array and convert it into regular expression

by plobsing (Friar)
on Dec 08, 2007 at 19:19 UTC ( [id://655868]=note: print w/replies, xml ) Need Help??


in reply to how can i split a string to an array and convert it into regular expression

If I understand correctly, every line on STDIN is a possible match in a regex you are building. Then one solution is this:
my @arry = <>; my $pattern = join('|', @arry); my $regex = eval("qr/$pattern/");
That of course assumes that your input isn't doing anything evil (like having a '/' in it).
  • Comment on Re: how can i split a string to an array and convert it into regular expression
  • Download Code

Replies are listed 'Best First'.
Re^2: how can i split a string to an array and convert it into regular expression
by ikegami (Patriarch) on Dec 08, 2007 at 19:31 UTC

    You forgot to strip the line endings and you needlessly invoke the Perl parser and compiler in addition to the regex parser and compiler when only the latter is needed.

    # If we are reading in regexs. chomp( my @arry = <> ); my $pattern = join('|', @arry); my $regex = qr/$pattern/;
    # If we are reading in text strings. chomp( my @arry = <> ); my $pattern = join('|', map quotemeta, @arry); my $regex = qr/$pattern/;
      I thought qr// was a compile time thing. I stand corrected.
      Thank you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-23 14:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found