Negative Regular Expression

R

rand007

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

Jürgen Exner

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
 
P

PoisonPen

Assuming you have $filename defined:

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



Jürgen Exner said:
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
 
R

rand007

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 said:
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
 
R

rand007

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.


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 said:
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
 
R

rand007

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.


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 said:
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
 
U

Uri Guttman

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top