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

regexp to check if file name matches pattern

by smanicka (Scribe)
on Feb 24, 2009 at 21:49 UTC ( [id://746115]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks,I have a file names of the pattern SYSBLAHBLAH%BLAHBLAH%BLAHBLAH%timedate.pdf

Could anyone help me write a regexp to check if a given filename matches this pattern?

I figure i have to say =~/^SYS something /i but i am not sure how exactly to match up with the first 2 percentage signs, the time date stamp is not of concern.Any help would be appreciated.

i need to do : if filename matches pattern, do action .else skip

i am attempting the regex as =~m/^SYS\w%\w%\w.*/i

Would this work? Thanks

- Sandhya
  • Comment on regexp to check if file name matches pattern

Replies are listed 'Best First'.
Re: regexp to check if file name matches pattern
by davido (Cardinal) on Feb 24, 2009 at 22:07 UTC

    Perhaps this:

    /^SYS(?:[^%]+%)+[^.]*\.pdf/i

    Matches SYS followed by any non % character(s) that are followed by a % (can be repeated), followed by just about anything except a dot (.) (that's where the date field goes), followed by dot pdf.

    Update: /i added, because when I see SYS and .pdf (upper, and lower) I start thinking that case probably shouldn't be allowed to foul up a match.


    Dave

      the systemblahblah can contain a period unfortunately

        I didn't preclude the dot from "systemblah%blah%", I precluded dot from the datestamp. Did you try it yet? There may be other errors, but that one isn't. ;)


        Dave

Re: regexp to check if file name matches pattern
by Marshall (Canon) on Feb 24, 2009 at 23:22 UTC
    An alternate solution (there are many):
    $file= "SYSB_A31.GROUPINS%RDEBLIST%REAPPLY%2008.12.16.20.31.35.pdf"; if ($file =~ /^SYS[\w.]+%[\w.]+%[\w.]+%[0-9.]+.pdf/) {print "yes"} else {print "no"} #prints yes
    A set of characters is enclosed in []. \w are the "word chars" and we add the "dot" to that set by \w.+. Same idea with 0-9.+. Other solution that has [^%]+ is good too. That means anything except %. Take your pick.
Re: regexp to check if file name matches pattern
by smanicka (Scribe) on Feb 24, 2009 at 22:24 UTC
    Hello Dave, it doesn't work.I tried:
    #!/usr/bin/perl $file= "SYSB_A31.GROUPINS%RDEBLIST%REAPPLY%2008.12.16.20.31.35.pdf"; if ($file=~ /^SYS(?:[^%]+%)+[^.]*\.pdf/i){ print "yes"; } else{ print "no"; }
    and it prints out no. Thanks Smanicka

      Lol, I had no way of knowing that your date field was "dot delimited." My solution would naturally reject that. Here's a new approach that should do the trick given that new, previously unmentioned requirement.

      /^SYS(?:[^%]+%){2,}[\d.]+\.pdf/i

      Let me know how that works out. This time I'm specifically allowing the 'dot' delimiter in the date field. This solution is going to result in some backtracking though. It might be better written as:

      /^SYS(?:[^%]+%){2,}(?:\d+)(?:.\d+)+\.pdf/i

      This will get less backtracking, and thus should be a little more efficient, if that matters.


      Dave

      tried {2,} instead of + as suggested.since prints out no
Re: regexp to check if file name matches pattern
by Grey Fox (Chaplain) on Feb 25, 2009 at 18:13 UTC
    Howdy;
    I have posted in the past a great tool for explaining regexe's called My REGEX Tester. I have a link to it in Found new online Regex tool.
    Plus there is a nice website called Regular-Expressions Quickstart. Which teaches all of the basics of REGEX.

    Teach a person to fish and you feed them for a lifetime.

    -- Grey Fox
    "We are grey. We stand between the darkness and the light" B5

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2025-03-22 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (63 votes). Check out past polls.