Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > FAQ 6.24 How do I match a regular expression that's in a variable?

Reply
Thread Tools

FAQ 6.24 How do I match a regular expression that's in a variable?

 
 
PerlFAQ Server
Guest
Posts: n/a
 
      04-19-2011
This is an excerpt from the latest version perlfaq6.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

6.24: How do I match a regular expression that's in a variable?


,
(contributed by brian d foy)

We don't have to hard-code patterns into the match operator (or anything
else that works with regular expressions). We can put the pattern in a
variable for later use.

The match operator is a double quote context, so you can interpolate
your variable just like a double quoted string. In this case, you read
the regular expression as user input and store it in $regex. Once you
have the pattern in $regex, you use that variable in the match operator.

chomp( my $regex = <STDIN> );

if( $string =~ m/$regex/ ) { ... }

Any regular expression special characters in $regex are still special,
and the pattern still has to be valid or Perl will complain. For
instance, in this pattern there is an unpaired parenthesis.

my $regex = "Unmatched ( paren";

"Two parens to bind them all" =~ m/$regex/;

When Perl compiles the regular expression, it treats the parenthesis as
the start of a memory match. When it doesn't find the closing
parenthesis, it complains:

Unmatched ( in regex; marked by <-- HERE in m/Unmatched ( <-- HERE paren/ at script line 3.

You can get around this in several ways depending on our situation.
First, if you don't want any of the characters in the string to be
special, you can escape them with "quotemeta" before you use the string.

chomp( my $regex = <STDIN> );
$regex = quotemeta( $regex );

if( $string =~ m/$regex/ ) { ... }

You can also do this directly in the match operator using the "\Q" and
"\E" sequences. The "\Q" tells Perl where to start escaping special
characters, and the "\E" tells it where to stop (see perlop for more
details).

chomp( my $regex = <STDIN> );

if( $string =~ m/\Q$regex\E/ ) { ... }

Alternately, you can use "qr//", the regular expression quote operator
(see perlop for more details). It quotes and perhaps compiles the
pattern, and you can apply regular expression flags to the pattern.

chomp( my $input = <STDIN> );

my $regex = qr/$input/is;

$string =~ m/$regex/ # same as m/$input/is;

You might also want to trap any errors by wrapping an "eval" block
around the whole thing.

chomp( my $input = <STDIN> );

eval {
if( $string =~ m/\Q$input\E/ ) { ... }
};
warn $@ if $@;

Or...

my $regex = eval { qr/$input/is };
if( defined $regex ) {
$string =~ m/$regex/;
}
else {
warn $@;
}



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FAQ 6.12 Can I use Perl regular expressions to match balanced text? PerlFAQ Server Perl Misc 0 04-19-2011 10:00 PM
FAQ 6.17 How do I efficiently match many regular expressions at once? PerlFAQ Server Perl Misc 0 01-19-2011 11:00 AM
FAQ 6.12 Can I use Perl regular expressions to match balanced text? PerlFAQ Server Perl Misc 0 01-09-2011 05:00 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments