Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Pattern match

by JFarr (Sexton)
on Jan 17, 2006 at 21:07 UTC ( #523839=perlquestion: print w/replies, xml ) Need Help??

JFarr has asked for the wisdom of the Perl Monks concerning the following question:

Helo Monks, I've been trying to match a string and keep failing. I have Rescue21_binaries_V5.0.29.0_patch_R5.0.24.1_DiffList.txt and want to match the words "Rescue21", "patch" and "DiffList.txt". I've tried
$mystring = "Rescue21_binaries_V5.0.29.0_patch_R5.0.24.1_DiffList.txt" +; if( $mystring =~ m/Rescue21\w_patch_\wDiffList.txt/ ) { }
But this keeps failing. Can someone smarter than I help with the reg exp match? Thanks

Replies are listed 'Best First'.
Re: Pattern match
by davido (Cardinal) on Jan 17, 2006 at 21:15 UTC

    The problem you have is that you're misunderstanding quantification of metacharacters, and metacharacters themselves. \w matches one "word" character, not a bunch of characters. \w also does not match "5.0.29.0", for example. Also, '.' matches any character except for newline, so it must be escaped, as in \. if you want it to be seen as a literal character in the pattern match. You probably want something more like this:

    m/Rescue21.+patch.+DiffList\.txt/

    ...or...

    { local $_ = $mystring; if( /Rescue21/ && /patch/ && /DiffList.txt/ ) { #.... } }

    ...though the latter would give a false positive if the filename looked like "DiffList.txt_patch_Rescue21". In other words, the latter solution wouldn't reject matches where the keywords appeared out of order.


    Dave

Re: Pattern match
by chromatic (Archbishop) on Jan 17, 2006 at 21:16 UTC

    \w only matches one character. It won't match literal dots. You might do better with m/Rescue21[\w\.]+_patch_[\w.]+DiffList.txt/. (I didn't test it.)

Re: Pattern match
by esskar (Deacon) on Jan 17, 2006 at 21:14 UTC
    $mystring = "Rescue21_binaries_V5.0.29.0_patch_R5.0.24.1_DiffList.txt" +; if( $mystring =~ m/Rescue21.*_patch_.*DiffList.txt/ ) { print "Matched\n"; }
    i suggest you read perldoc perlretut and perldoc perlre
Re: Pattern match
by SamCG (Hermit) on Jan 17, 2006 at 21:46 UTC
    I might add the "Difflist.txt" portion of your pattern will match Difflist.txt, Difflistntxt, or Difflist&txt. If you mean a literal dot, you either need to escape it or (as another poster did) put it into a character class. A character class is a grouping of possible characters you're looking for, surrounded by square brackets. [0-9.], for example, would look for any digit or the literal period.
Re: Pattern match
by ercparker (Hermit) on Jan 18, 2006 at 00:33 UTC
    you can also check that it matches the characters you're looking for followed by one or more non-space, such as:
    my $mystring = "Rescue21_binaries_V5.0.29.0_patch_R5.0.24.1_DiffList.t +xt"; if($mystring =~ m/Rescue21[^\s]+?patch[^\s]+?DiffList.txt/ ) { print "Matched" . "\n"; }
    hope that helps

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2023-05-30 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?