Negation of RegEx

Discussion in 'Perl Misc' started by Dan, Jul 8, 2004.

  1. Dan

    Dan Guest

    Hi everybody,

    I have a special problem as the following:

    $sentence="a hard problem";

    I want to build a regular expression for searching there is
    no word "hard" in the above sentence.
    Usually, we could try $sentence !~/RegEx/ for this problem.
    But, I am not allowed to use "!~", I can only use "=~" for this
    problem, which means that /RegEx/ is a pattern for searching no
    "hard" in a sentence.

    Is there anyone who konws the solution of this problem?


    Thanks in advance


    Dan
     
    Dan, Jul 8, 2004
    #1
    1. Advertising

  2. > I have a special problem as the following:
    >
    > $sentence="a hard problem";
    >
    > I want to build a regular expression for searching there is
    > no word "hard" in the above sentence.
    > Usually, we could try $sentence !~/RegEx/ for this problem.
    > But, I am not allowed to use "!~", I can only use "=~" for this
    > problem, which means that /RegEx/ is a pattern for searching no
    > "hard" in a sentence.
    >
    > Is there anyone who konws the solution of this problem?


    Two weeks ago this problem was discussed already.
    Subject: REGEX negation
    First read this.


    --
    Andre

    ---

    Fighting for peace is like f..ing for virginity
     
    Andre Wisniewski, Jul 8, 2004
    #2
    1. Advertising

  3. Dan

    Dan Guest

    Randal's answer /^(?!FOO)/can just deals with a special case in which
    the unwanted word is at the beginning of a sentence.

    What if the unwanted word could appear at any position of the
    sentence?
    The look-ahead negtive assertion doesn't work. Just give it a try!

    So any other solutions?

    I have a detour solution here

    $sentence= "I have a problem";
    $sentence=~/(?(?=.*\bhave\b) <something never match>(?{print "True
    Branch\n";})| <something always match> (?{print "False Branch\n";}))/;

    This is a if () THEN {} ELSE {} statement.
    The condition of if is a look-ahead positive assertion, which works
    correctly.
    But the problem is when it goes to True branch and has an unmatch
    there, a wierd thing happens: it goes to FALSE BRANCH.

    if the logic in THEN is ture, it should go out of the statement and
    not go into
    ELSE branch. This is what I can't understand

    Hope someone would solve this problem.

    Dan


    >>Andreas> is it possible to construct a single regular expression

    which matches
    >>Andreas> everything except strings which start with a certain

    string?

    >>The regex


    >> /^(?!FOO)/


    >>matches everything except lines that start with FOO.


    >>print "Just another Perl hacker,"; # and the first one, at that







    "Andre Wisniewski" <> wrote in message news:<opsat1fwpdudlqzq@ares>...
    > > I have a special problem as the following:
    > >
    > > $sentence="a hard problem";
    > >
    > > I want to build a regular expression for searching there is
    > > no word "hard" in the above sentence.
    > > Usually, we could try $sentence !~/RegEx/ for this problem.
    > > But, I am not allowed to use "!~", I can only use "=~" for this
    > > problem, which means that /RegEx/ is a pattern for searching no
    > > "hard" in a sentence.
    > >
    > > Is there anyone who konws the solution of this problem?

    >
    > Two weeks ago this problem was discussed already.
    > Subject: REGEX negation
    > First read this.
     
    Dan, Jul 8, 2004
    #3
  4. Dan

    Keith Keller Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On 2004-07-08, Dan <> wrote:
    > But, I am not allowed to use "!~", I can only use "=~" for this
    > problem,


    Why are you not allowed to use !~? Sounds like homework to me.

    - --keith

    - --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.3 (GNU/Linux)

    iD8DBQFA7de+hVcNCxZ5ID8RAmYuAJ0SHm1aUirkhpKPU+WdMHoaZnxopwCggvpE
    4krTCU8/idIytSlF109dDes=
    =NaGl
    -----END PGP SIGNATURE-----
     
    Keith Keller, Jul 9, 2004
    #4
  5. Dan

    fifo Guest

    At 2004-07-08 15:51 -0700, Dan wrote:
    > Randal's answer /^(?!FOO)/can just deals with a special case in which
    > the unwanted word is at the beginning of a sentence.
    >
    > What if the unwanted word could appear at any position of the
    > sentence?
    > The look-ahead negtive assertion doesn't work. Just give it a try!
    >


    /^(?!.*word)/s will match if and only if /word/ doesn't.
     
    fifo, Jul 9, 2004
    #5
  6. Dan

    Dan Guest

    I will put regular expressions in a script. No sign "!~" or "=~" allowed in there.

    Dan

    Keith Keller <-francisco.ca.us> wrote in message news:<-francisco.ca.us>...
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > On 2004-07-08, Dan <> wrote:
    > > But, I am not allowed to use "!~", I can only use "=~" for this
    > > problem,

    >
    > Why are you not allowed to use !~? Sounds like homework to me.
    >
    > - --keith
    >
    > - --
    > -francisco.ca.us
    > (try just my userid to email me)
    > AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom
    >
    > -----BEGIN PGP SIGNATURE-----
    > Version: GnuPG v1.2.3 (GNU/Linux)
    >
    > iD8DBQFA7de+hVcNCxZ5ID8RAmYuAJ0SHm1aUirkhpKPU+WdMHoaZnxopwCggvpE
    > 4krTCU8/idIytSlF109dDes=
    > =NaGl
    > -----END PGP SIGNATURE-----
     
    Dan, Jul 9, 2004
    #6
  7. Dan

    Keith Keller Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On 2004-07-09, Dan <> wrote:

    [TOFU snipped]

    > Keith Keller <-francisco.ca.us> wrote in message news:<-francisco.ca.us>...


    >> On 2004-07-08, Dan <> wrote:
    >> > But, I am not allowed to use "!~", I can only use "=~" for this
    >> > problem,

    >>
    >> Why are you not allowed to use !~? Sounds like homework to me.


    > I will put regular expressions in a script. No sign "!~" or "=~"
    > allowed in there.


    Above you said you could use =~. Perhaps you can clarify your
    needs more thoroughly?

    - --keith

    - --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.3 (GNU/Linux)

    iD8DBQFA7u1ghVcNCxZ5ID8RApzjAJ9bfWxhNnQXnvhuZ8i85rQ6HAKlZwCgi74k
    I1Bk5YENJOfknoO4mObD9vE=
    =3cj7
    -----END PGP SIGNATURE-----
     
    Keith Keller, Jul 9, 2004
    #7
  8. [ Please learn (and then use) the proper form for a followup posting ]


    Dan <> wrote:
    > Keith Keller <-francisco.ca.us> wrote in message news:<-francisco.ca.us>...
    >> On 2004-07-08, Dan <> wrote:
    >> > But, I am not allowed to use "!~", I can only use "=~" for this
    >> > problem,

    >>
    >> Why are you not allowed to use !~? Sounds like homework to me.
    >>

    > I will put regular expressions in a script.
    > No sign "!~" or "=~" allowed in there.



    Why is no "!~" or "=~" allowed in there?

    I use them in my "scripts" all the time.


    If you don't tell us what your real problem is, then we probably
    won't be able to help you solve it...


    [ snip TOFU ]

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 10, 2004
    #8
  9. Dan

    Dan Guest

    I think the only way out is build a negation of RegEx.
    It is actually a subpatten of a larger regular expression.
    can't be any other formats. :-(


    Thanks

    Dan



    Tad McClellan <> wrote in message news:<>...
    > [ Please learn (and then use) the proper form for a followup posting ]
    >
    >
    > Dan <> wrote:
    > > Keith Keller <-francisco.ca.us> wrote in message news:<-francisco.ca.us>...
    > >> On 2004-07-08, Dan <> wrote:
    > >> > But, I am not allowed to use "!~", I can only use "=~" for this
    > >> > problem,
    > >>
    > >> Why are you not allowed to use !~? Sounds like homework to me.
    > >>

    > > I will put regular expressions in a script.
    > > No sign "!~" or "=~" allowed in there.

    >
    >
    > Why is no "!~" or "=~" allowed in there?
    >
    > I use them in my "scripts" all the time.
    >
    >
    > If you don't tell us what your real problem is, then we probably
    > won't be able to help you solve it...
    >
    >
    > [ snip TOFU ]
     
    Dan, Jul 10, 2004
    #9
  10. Dan <> wrote:

    [ Please learn (and then use) the proper form for a followup posting.
    Soon!
    ]
    > can't be any other formats. :-(



    Why can't it be in any other format?



    [ snip yet more TOFU. Sheesh! ]

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 10, 2004
    #10
  11. Dan

    Dan Guest

    Tad McClellan <> wrote in message news:<>...
    > Dan <> wrote:
    >
    > [ Please learn (and then use) the proper form for a followup posting.
    > Soon!
    > ]
    > > can't be any other formats. :-(

    >
    >
    > Why can't it be in any other format?
    >
    >
    >
    > [ snip yet more TOFU. Sheesh! ]


    The negation should be right in the boundary "/Negation of RegEx/".
    Do you have any solution for that?

    Thanks

    Dan
     
    Dan, Jul 11, 2004
    #11
  12. [posted & mailed]

    On 11 Jul 2004, Dan wrote:

    >The negation should be right in the boundary "/Negation of RegEx/".
    >Do you have any solution for that?


    Hasn't the solution already been provided?

    /^(?!.*foo)/s is the negation of the pattern /foo/.

    --
    Jeff "japhy" Pinyan % How can we ever be the sold short or
    RPI Acacia Brother #734 % the cheated, we who for every service
    RPI Corporation Secretary % have long ago been overpaid?
    http://japhy.perlmonk.org/ %
    http://www.perlmonks.org/ % -- Meister Eckhart
     
    Jeff 'japhy' Pinyan, Jul 12, 2004
    #12
    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. George Sakkis

    Negation in regular expressions

    George Sakkis, Sep 8, 2006, in forum: Python
    Replies:
    6
    Views:
    544
  2. joshc

    unary negation operator question

    joshc, Apr 1, 2005, in forum: C Programming
    Replies:
    17
    Views:
    600
    Keith Thompson
    Apr 1, 2005
  3. jimmij

    negation operator !

    jimmij, Dec 8, 2006, in forum: C++
    Replies:
    3
    Views:
    824
    John Carson
    Dec 9, 2006
  4. Andreas Hochsteger

    Negation of RegEx

    Andreas Hochsteger, Apr 21, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    100
    Randal L. Schwartz
    Apr 21, 2004
  5. Rusty Phillips

    REGEX Negation

    Rusty Phillips, Jun 23, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    259
    Rusty Phillips
    Jun 24, 2004
Loading...

Share This Page