Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Negative Regular Expression

Reply
Thread Tools

Negative Regular Expression

 
 
rand007@gmail.com
Guest
Posts: n/a
 
      12-25-2006
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.

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      12-25-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
PoisonPen
Guest
Posts: n/a
 
      12-25-2006
Assuming you have $filename defined:

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



Jürgen Exner wrote:
> (E-Mail Removed) 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 woulda
> 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


 
Reply With Quote
 
rand007@gmail.com
Guest
Posts: n/a
 
      12-25-2006
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ürgen Exner wrote:
> (E-Mail Removed) 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 woulda
> 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


 
Reply With Quote
 
rand007@gmail.com
Guest
Posts: n/a
 
      12-25-2006
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.


(E-Mail Removed) wrote:
> 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ürgen Exner wrote:
> > (E-Mail Removed) 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


 
Reply With Quote
 
rand007@gmail.com
Guest
Posts: n/a
 
      12-25-2006
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.


(E-Mail Removed) wrote:
> 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ürgen Exner wrote:
> > (E-Mail Removed) 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


 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      12-26-2006
>>>>> "r" == rand007 <(E-Mail Removed)> 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 ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Negative setup and Negative hold prem_eda VHDL 5 10-11-2004 12:14 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments