logo       


Choosing A Webhost:
A web hosting service is a type of Internet hosting service that allows individuals and organizations to provide their own website accessible via the World Wide Web. Web hosts are companies that provide space on a server they own for use by their clients as well as providing Internet connectivity, typically in a data center. Web hosts can also provide data center space and connectivity to the Internet for servers they do not own to be located in their data center, called colocation. more...

perlfaq6: How do I match a pattern that is supplied by the user?: msg#00000

Subject: perlfaq6: How do I match a pattern that is supplied by the user?
Instead of a patch which shows a big deletion and a big addition,
here's  the text that completely replaces the old answer.

Updates:
   * Change question to "How do I match a regular expression that's in
a variable?" (perlfaq.pod will be updated  too)
   * added bit about quotemeta()
   * added bit about qr//

=head2 How do I match a regular expression that's in a variable?
X<regex, in variable> X<eval> X<regex> X<quotemeta> X<\Q, regex>
X<\E, regex>, X<qr//>

(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 C<$regex>.
Once you have the pattern in C<$regex>, you use that variable in the
match operator.

    chomp( my $regex = <STDIN> );
    
    if( $string =~ m/$regex/ ) { ... }

Any regular expression special characters in C<$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 C<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 C<\Q>
and C<\E> sequences. The C<\Q> tells Perl where to start escaping
special characters, and the C<\E> tells it where to stop (see L<perlop>
for more details).

    chomp( my $regex = <STDIN> );
   
    if( $string =~ m/\Q$regex\E/ ) { ... }

Alternately, you can use C<qr//>, the regular expression quote operator
(see
L<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 C<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 $@;
      }



Ruby Jobs
Java Jobs
Jobs in California
more...
what
job title, keywords
where
city, state, zip
jobs by job search
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
db.firebase.por...    text.xml.xalan....    qnx.openqnx.dev...    user-groups.zar...    internationaliz...    kde.devel.konve...    finance.e-gold....    emacs.latex.pre...    gis.therion/200...    web.webmin.gene...    yellowdog.gener...    vserver/2003-08...    redhat.release....    sysutils.tivoli...    xfree86.expert/...    mail.becky.user...    hardware.netapp...    netbsd.ports.xe...    python.distutil...    boot-loaders.gr...    culture.interne...    java.springfram...    activedir/2006-...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe