On top of the answers given, let me critique your regex a bit.  

First off, be aware that the m at the beginning of your regular expression
is implied, and can be left off.  It's okay to put it there if it is easier
for you to read, but don't be thrown off if people leave it out.

Secondly,let's look at your regex.  It basically says...

m/\(\^\.\.gif\|\^\.\.jpg)/\

m/      Match
\(      one (
\^      followed by '^'
\.\.    followed by '..'
gif     followed by 'gif'
\|      followed by '|'
\^      followed by '^'
\.\.    followed by '..'
jpg     followed by 'jpg'  
)       (a right parenthesis with no matching left)
/       End Match
\       (an extra '\')

The main thing that I see here is that you seem to be escaping almost every
character.  Remember:  characters immediately following a backslash lose
their special meaning in a regex.  The above regex would sort of match the
following string:  '(^..gif|^..jpg'.  But what I think you were trying to do
(correct me if I'm wrong) was something more like this:

  m/(^.+\.gif|^.+\.jpg)/

which would still be wrong, because you can't put anchors and such in a
regex, but if we move everything except the characters we want to compare
out of the (|), we get:

  m/^.+\.(gif|jpg)$/i

which means roughly this:

m/        Beginning of match
^         Starting at the beginning of the string
..+        Match one or more of any character
\.        Followed by a '.'
(gif|jpg) Followed by either 'gif' or 'jpg'
$         Followed by the end of the string
/         End of match
i         Match upper or lower case letters

One final note:  I chose .+ instead of .* (match zero or more of any
character) because legitemate picture files should have a filename and an
extension, but it's up to you whether you want to match a file simply called
'.gif' or '.jpg'.  It could happen.


-----Original Message-----
From: Toby Stuart
To: '[EMAIL PROTECTED]'
Sent: 7/2/02 11:07 PM
Subject: RE: Regex help!

#!perl.exe -w

use strict;

my $filename = 'image.gif';
#my $filename = 'image.jpg';

if ($filename =~ m/^.*\.(jpg|gif)$/)
{
        print "Got one!";
}

---

hth

toby

-----Original Message-----
From: Troy May [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, July 03, 2002 3:58 PM
To: Perl Beginners
Subject: Regex help!


Please...  :)

Hello, I'm trying to match images by the extension.  Either .jpg or .gif

Here's what I wrote:

$plup =~ m/\(\^\.\.gif\|\^\.\.jpg)/\

Is it all screwed up?  I'm not real good with regexes.

Thanks if you can help me!

Troy

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to