Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Negation of RegEx

Reply
Thread Tools

Negation of RegEx

 
 
Dan
Guest
Posts: n/a
 
      07-08-2004
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
 
Reply With Quote
 
 
 
 
Andre Wisniewski
Guest
Posts: n/a
 
      07-08-2004
> 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
 
Reply With Quote
 
 
 
 
Dan
Guest
Posts: n/a
 
      07-08-2004
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" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Keith Keller
Guest
Posts: n/a
 
      07-08-2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2004-07-08, Dan <(E-Mail Removed)> 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

- --
http://www.velocityreviews.com/forums/(E-Mail Removed)-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+WdMH oaZnxopwCggvpE
4krTCU8/idIytSlF109dDes=
=NaGl
-----END PGP SIGNATURE-----
 
Reply With Quote
 
fifo
Guest
Posts: n/a
 
      07-09-2004
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.
 
Reply With Quote
 
Dan
Guest
Posts: n/a
 
      07-09-2004
I will put regular expressions in a script. No sign "!~" or "=~" allowed in there.

Dan

Keith Keller <(E-Mail Removed)-francisco.ca.us> wrote in message news:<(E-Mail Removed)-francisco.ca.us>...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 2004-07-08, Dan <(E-Mail Removed)> 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
>
> - --
> (E-Mail Removed)-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+WdMH oaZnxopwCggvpE
> 4krTCU8/idIytSlF109dDes=
> =NaGl
> -----END PGP SIGNATURE-----

 
Reply With Quote
 
Keith Keller
Guest
Posts: n/a
 
      07-09-2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2004-07-09, Dan <(E-Mail Removed)> wrote:

[TOFU snipped]

> Keith Keller <(E-Mail Removed)-francisco.ca.us> wrote in message news:<(E-Mail Removed)-francisco.ca.us>...


>> On 2004-07-08, Dan <(E-Mail Removed)> 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

- --
(E-Mail Removed)-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)

iD8DBQFA7u1ghVcNCxZ5ID8RApzjAJ9bfWxhNnQXnvhuZ8i85r Q6HAKlZwCgi74k
I1Bk5YENJOfknoO4mObD9vE=
=3cj7
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-09-2004

[ Please learn (and then use) the proper form for a followup posting ]


Dan <(E-Mail Removed)> wrote:
> Keith Keller <(E-Mail Removed)-francisco.ca.us> wrote in message news:<(E-Mail Removed)-francisco.ca.us>...
>> On 2004-07-08, Dan <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Dan
Guest
Posts: n/a
 
      07-10-2004
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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>.. .
> [ Please learn (and then use) the proper form for a followup posting ]
>
>
> Dan <(E-Mail Removed)> wrote:
> > Keith Keller <(E-Mail Removed)-francisco.ca.us> wrote in message news:<(E-Mail Removed)-francisco.ca.us>...
> >> On 2004-07-08, Dan <(E-Mail Removed)> 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 ]

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-10-2004
Dan <(E-Mail Removed)> 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
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
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
negation operator ! jimmij C++ 3 12-09-2006 10:42 AM
Negation in regular expressions George Sakkis Python 6 09-08-2006 02:44 PM
unary negation operator question joshc C Programming 17 04-01-2005 07:52 PM
REGEX Negation Rusty Phillips Perl Misc 2 06-24-2004 09:16 PM
Negation of RegEx Andreas Hochsteger Perl Misc 3 04-21-2004 09:11 PM



Advertisments