Negative Regular Expression

Discussion in 'Perl Misc' started by rand007@gmail.com, Dec 25, 2006.

  1. Guest

    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.
     
    , Dec 25, 2006
    #1
    1. Advertising

  2. 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
     
    Jürgen Exner, Dec 25, 2006
    #2
    1. Advertising

  3. PoisonPen Guest

    Assuming you have $filename defined:

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



    Jürgen Exner wrote:
    > 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
     
    PoisonPen, Dec 25, 2006
    #3
  4. Guest

    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:
    > 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
     
    , Dec 25, 2006
    #4
  5. Guest

    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.


    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:
    > > 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
     
    , Dec 25, 2006
    #5
  6. Guest

    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.


    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:
    > > 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
     
    , Dec 25, 2006
    #6
  7. Uri Guttman Guest

    >>>>> "r" == rand007 <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Dec 26, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,303
  2. Jason Friedman

    Re: Regular expression negative look-ahead

    Jason Friedman, Jul 2, 2013, in forum: Python
    Replies:
    0
    Views:
    86
    Jason Friedman
    Jul 2, 2013
  3. Ian Kelly
    Replies:
    0
    Views:
    98
    Ian Kelly
    Jul 2, 2013
  4. Jason Friedman

    Regular expression negative look-ahead

    Jason Friedman, Jul 2, 2013, in forum: Python
    Replies:
    1
    Views:
    71
    Neil Cerutti
    Jul 2, 2013
  5. Jason Friedman

    Re: Regular expression negative look-ahead

    Jason Friedman, Jul 4, 2013, in forum: Python
    Replies:
    0
    Views:
    75
    Jason Friedman
    Jul 4, 2013
Loading...

Share This Page