Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

pattern-matching examples

by root (Scribe)
on Nov 12, 1999 at 02:09 UTC ( #969=perltutorial: print w/ replies, xml ) Need Help??

/Lisa|Bart/ #matches either Lisa or Bart by using the | (or) operator /Lisa\sBart/ #matches Lisa then a whitespace character and then Bart /\w{5}/ #matches 5 alphanumeric/word characters in a row /sub\s+\w*{/ #matches the word sub then one or more spaces and then #0 or more alphanumeric characters followed by a { /\d{3}-\d{4} #matches a phone number of the form 555-1234 #3 numbers followed by a - followed by 4 numbers
A great way to test just what a given pattern matches is the program below:

#!/usr/bin/perl
while(<>){
    print if m!
your pattern here!;
}

All you have to do is drop your test pattern into place. Then run the program and type a line of text in at a time. If the pattern matches it will echo the line of text to the screen. When you have played with it enough simply type Ctrl-C.

You should now take some time to learn about split and join

Comment on pattern-matching examples
Download Code
RE: pattern-matching examples
by Anonymous Monk on Mar 23, 2000 at 01:22 UTC
    It would be really nice if you would have expamples use the /g option at the end of the reg. expr. A lot of people I know avoid using the $_ character and instead like to use a variable assignment with the =~ explicitly. Thanks P.S. Good site otherwise, but your Reg Exp. section could use more expamples.
      #!/usr/bin/perl -w # # Here is a CGI example of using a regexp to parse a query string, # when you're not sure exactly what will be in the query string # Put it in you web space, and call it with # http://host/script?param_1=this&param_2=that&the_other=doesntmatchth +emask # or call it with ?error=HelloWebMaster if you want to fill the error +log with garbage # use CGI qw( :standard :HTML ); # set slay typos use strict; # read in the CGI params my $object = new CGI(); # print our HTML header print header(),h1( "params: " ); my $key; # set our variable mask. This part gets thrown out. # At makeyourbanner.com, I use more than one pattern, # so that I can determine the number of text areas and style settings +dynamically. # in CGI programming, this is very handy if you are designing a backen +d, # and don't have advanced knowledge of how the front end will call it. my $mask = "param_"; foreach $key ( $object->param() ) { if ( $key =~ /^($mask)(.+)/ ) { #found a parameter, print it out print "<P>param $2=", $object->param( $key ), "</P>"; } elsif ( $key =~ /error/i ) { #found error flag, print error to the appache logs (you ARE us +ing apache, right?!) print STDERR "error: found $key=", $object->param( $ke +y ), " in $0\n"; } }
Re: pattern-matching examples
by witandhumor (Pilgrim) on Mar 28, 2004 at 04:34 UTC
    I think the last example /\d{3}-\d{4} should really be /\d{3} -\d{4}/

    I figure a newbie may get confused if they try this one...

Re: pattern-matching examples
by Anonymous Monk on Aug 17, 2004 at 06:50 UTC
    intesting

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2014-12-20 17:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (97 votes), past polls