Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Hlelp regarding regex

by wind (Priest)
on Jun 02, 2011 at 02:25 UTC ( [id://907732]=note: print w/replies, xml ) Need Help??


in reply to Hlelp regarding regex

Your specification is extremely unclear. Could you possibly explain what exactly you're wanting to verify about the Y character in the string?

If you have trouble explaining what you want, you should at least give a list of examples of strings that you do want to match, and a counter list of failed matches.

The below regex matches your string, but given your unclear spec, it probably isn't useful.

my $x = "\\\\files\\builds\\data\\M9998SBQCACSYD30401S"; if ($x =~ /Y\D\d+\D*$/){ print "This matches something, don't know if it's really what you +want"; }

Replies are listed 'Best First'.
Re^2: Hlelp regarding regex
by Anonymous Monk on Jun 02, 2011 at 04:08 UTC

    Sorry for the unclear spec...Hope the below is clear

    Spec:- After the last "\" $x should match the pattern Y(digit)(number).The reason is "$x" is given as an option by the user and we use the letter to distinguish the location of some files,we only want to process locations with letter Y.Can you please help?

    my $x = "\\\\files\\builds\\data\\M9998SBQCACSYD30401S";-->should matc +h my $x = "\\\\files\\builds\\data\\M9998SBQCACSAD30401S";-->Should not +match
      $x should match the pattern Y(digit)(number)
      Surely you mean Y(letter)(number)?

      Try this:

      $x =~ /Y[A-Z]\d+[^\d\\]*$/
      as in
      for my $x ("\\\\files\\builds\\data\\M9998SBQCACSYD30401S", "\\\\file +s\\builds\\data\\M9998SBQCACSAD30401S") { if($x =~ /Y[A-Z]\d+[^\d\\]*$/) { print "$x matches\n"; } else { print "$x doesn't match\n"; } }
      which yields:
      \\files\builds\data\M9998SBQCACSYD30401S matches \\files\builds\data\M9998SBQCACSAD30401S doesn't match

      ([^\d\\] makes sure you matched the last number, and no backslash follows.)

      Ideally, you'd provide a list of more than 2 strings, but your description helps. The following will probably suit your needs:
      my @list = ( # Good "\\\\files\\builds\\data\\M9998SBQCACSYD30401S", # Bad "\\\\files\\builds\\data\\M9998SBQCACSAD30401S", ); for my $x (@list) { if ($x =~ /Y[A-Z]\d[^\\]*$/){ print "$x - matches\n"; } else { print "$x - doesn't match\n"; } }
      Given that these are file paths, I personally would rely on File::Basename or File::Spec to separate the filenames from the directories before doing the tests. That way the code would still work on other platforms.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-25 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found