For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > December 2006 > Negative Regular Expression









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Negative Regular Expression
rand007@gmail.com

2006-12-25, 8:03 am

Hi,

I am trying to write a simple regular expression that returns a match
for each
string that does not contain specific suffix (e.g. all file names that
do not end with ".txt" extension).

I tried various negative look ahead assertions such as /.+(?!\.txt)/,
/.+(?!\.txt$)/,
/([a-z]|(A-Z)|[0-9])(?!\.txt$)/ and much more, but all of them does not
achieve this specific purpose.

Is there a way to accomplish this task with perl regular expressions?

Thanks,
Ran.

Jürgen Exner

2006-12-25, 8:03 am

rand007@gmail.com wrote:
> Hi,
>
> I am trying to write a simple regular expression that returns a match
> for each
> string that does not contain specific suffix (e.g. all file names that
> do not end with ".txt" extension).
>
> I tried various negative look ahead assertions such as /.+(?!\.txt)/,
> /.+(?!\.txt$)/,
> /([a-z]|(A-Z)|[0-9])(?!\.txt$)/ and much more, but all of them does
> not achieve this specific purpose.
>
> Is there a way to accomplish this task with perl regular expressions?


Is there a specific reason why you are making the task difficult or would a
simple solution work, too?

use File::Basename;
($name,$path,$suffix) = fileparse($fullname,@suffixlist);
if ($suffix ne 'txt') {
#whatever you want to do with that file
}

jue


PoisonPen

2006-12-25, 8:03 am

Assuming you have $filename defined:

if ($filename !~ /\.txt$/) {
# do whatever with this file
}



J=FCrgen Exner wrote:
> rand007@gmail.com wrote:
>
> Is there a specific reason why you are making the task difficult or would=

a
> simple solution work, too?
>
> use File::Basename;
> ($name,$path,$suffix) =3D fileparse($fullname,@suffixlist);
> if ($suffix ne 'txt') {
> #whatever you want to do with that file
> }
>=20
> jue


rand007@gmail.com

2006-12-25, 8:03 am

Hi Jue.

Thanks for the quick response.

You are absolutely right that the file suffix extension I mentioned is
a specific example that can be easily solved with your suggested code,
but what I am looking for is a generic capability for matching strings
that do not end with certains suffix as part of regular expressions
engine capabilities.

Why?
Because I want one engine that can perform this generic task on any
string input without
writing dedicated code for each task.

Ran.

for
J=FCrgen Exner wrote:
> rand007@gmail.com wrote:
>
> Is there a specific reason why you are making the task difficult or would=

a
> simple solution work, too?
>
> use File::Basename;
> ($name,$path,$suffix) =3D fileparse($fullname,@suffixlist);
> if ($suffix ne 'txt') {
> #whatever you want to do with that file
> }
>=20
> jue


rand007@gmail.com

2006-12-25, 8:03 am

Guys,

I found the magic look ahead expression that achieves my whish!!!
it goes like this: /^(?!.*strsuffix$)/

This expression matches any string that does not end with "srtsuffix".

Thanks all.
Ran.


rand007@gmail.com wrote:[color=darkred]
> Hi Jue.
>
> Thanks for the quick response.
>
> You are absolutely right that the file suffix extension I mentioned is
> a specific example that can be easily solved with your suggested code,
> but what I am looking for is a generic capability for matching strings
> that do not end with certains suffix as part of regular expressions
> engine capabilities.
>
> Why?
> Because I want one engine that can perform this generic task on any
> string input without
> writing dedicated code for each task.
>
> Ran.
>
> for
> J=FCrgen Exner wrote:
ld a[color=darkred]

rand007@gmail.com

2006-12-25, 8:03 am

Guys,

I found the magic look ahead expression that achieves my whish!!!
it goes like this: /^(?!.*strsuffix$)/

This expression matches any string that does not end with "srtsuffix".

Thanks all.
Ran.


rand007@gmail.com wrote:[color=darkred]
> Hi Jue.
>
> Thanks for the quick response.
>
> You are absolutely right that the file suffix extension I mentioned is
> a specific example that can be easily solved with your suggested code,
> but what I am looking for is a generic capability for matching strings
> that do not end with certains suffix as part of regular expressions
> engine capabilities.
>
> Why?
> Because I want one engine that can perform this generic task on any
> string input without
> writing dedicated code for each task.
>
> Ran.
>
> for
> J=FCrgen Exner wrote:
ld a[color=darkred]

Uri Guttman

2006-12-26, 4:10 am

>>>>> "r" == rand007 <rand007@gmail.com> writes:

first off don't top post. read the group guidelines which are posted
regularly.

r> I found the magic look ahead expression that achieves my whish!!!
r> it goes like this: /^(?!.*strsuffix$)/

r> This expression matches any string that does not end with "srtsuffix".

that is a negative lookahead and it may work here but it not the same as
a negative match which is probably what you want and which others have
shown you.

also that regex is anchored at both ends which is not needed. it uses a
fixed 'strsuffix' which is not a variable. if the suffix has a . in it
and that is not escaped than it will fail with bar.xstrsuffix. my point
is that you seemed to have lucked into this solution without
understanding it nor getting the responses. read perlre more and learn
about how to properly match a suffix or a set of them.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com