Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Regex for matching a file name

by Anonymous Monk
on Sep 06, 2004 at 23:41 UTC ( #388881=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, my web site is down and I can't figure out this problem:

I have an array with five file names which are:
11x0.htm
12x0.htm
1x0.htm
20x0.htm
8x0.htm


I have a variable called $THREAD, if thread equal "1" during a foreach loop I'm trying to use pattern matching to find the file that begins with 1, not 11, not 12, but 1 which in this case is "1x0.htm". The file at somepoint might be named 1x2, 1x12, ect. But the key is held in the $THREAD variable.

My pattern matching is not working:

I've tried everything, I thought this should work:

$THREAD= "1"
if ($filedir =~ /$THREADx*.htm/) {

Thanks in advance for any help.

Edited by Chady -- retitled from 'Emergency Help'

Replies are listed 'Best First'.
Re: Regex for matching a file name
by etcshadow (Priest) on Sep 06, 2004 at 23:45 UTC
    Make your regexp:
    /^${THREAD}x.*\.htm$/
    (emergency question gets somewhat curt, right-to-the point response)
    ------------ :Wq Not an editor command: Wq
Re: Regex for matching a file name
by shenme (Priest) on Sep 06, 2004 at 23:57 UTC
    First, you must explicitly limit what the variable name is that is being substituted, e.g.
      /${THREAD}x*.htm/
    otherwise you are trying to interpolate variable "$THREADx".

    Then you need to make sure the '.' is matching a period. Normally a '.' in an RE is a "match anything" character. So you would need to escape the magic meaning, e.g.
      /${THREAD}x*\.htm/

    Then you want the "match anything" for "any length" where you had the asterisk, e.g.
      /${THREAD}x.*\.htm/

    And... you might want to make sure you don't match "321xBOOM.htm" by saying the string '1' should come at the start of the string, e.g.
      /^${THREAD}x.*\.htm/

    And you might want to wrap it up by checking that the filename _ends_ with the 'htm', e.g.
      /^${THREAD}x.*\.htm$/

    Let us know if this helps!

      Thanks to both you Monks, the code works perfectly!!! I certainly appreciate your fast responses.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2019-10-22 00:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?