> -----Original Message----- > From: Ahmed Moustafa [mailto:[EMAIL PROTECTED]] > Subject: regular expression to get a file extension > > I had the following regular expression to get the extension > from a file > name: > > <code> > $extension = $filename; > $extension =~ s/(^.+\.)([^\.]+)$/$2/; > </code> > > But it didn't work (in case the $filename didn't have an > extension); so > I had to add the following line: > > <code> > $extension = "" if (!$1); > </code> > > What is wrong with my regular expression? > How can it be done in a single regular expression? > > -- > Ahmed Moustafa > http://pobox.com/~amoustafa >
Try this: $extension =~ s/(^.+\.?)([^\.]*)$/$2/; Your regex will fail for a couple of different reasons: 1) if there is no extension, there (probably) won't be a dot, so you need to take that into account (\.?). 2) if there is no extension, you're going to fail because you are matching "one or more non-period characters. You need "zero or more non-period characters", so change the final '+' to a '*'. That should take care of your problem. Hope this helps... Jason CONFIDENTIALITY NOTICE: ************************************************************************ The information contained in this ELECTRONIC MAIL transmission is confidential. It may also be privileged work product or proprietary information. This information is intended for the exclusive use of the addressee(s). If you are not the intended recipient, you are hereby notified that any use, disclosure, dissemination, distribution [other than to the addressee(s)], copying or taking of any action because of this information is strictly prohibited. ************************************************************************ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]