Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Hlelp regarding regex

by Anonymous Monk
on Jun 02, 2011 at 04:08 UTC ( [id://907740]=note: print w/replies, xml ) Need Help??


in reply to Re: Hlelp regarding regex
in thread Hlelp regarding regex

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

Replies are listed 'Best First'.
Re^3: Hlelp regarding regex
by bart (Canon) on Jun 02, 2011 at 08:39 UTC
    $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.)

Re^3: Hlelp regarding regex
by wind (Priest) on Jun 02, 2011 at 15:25 UTC
    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://907740]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-24 03:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found