Negation of RegEx

D

Dan

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
 
A

Andre Wisniewski

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.
 
D

Dan

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

 
K

Keith Keller

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

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 address 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+WdMHoaZnxopwCggvpE
4krTCU8/idIytSlF109dDes=
=NaGl
-----END PGP SIGNATURE-----
 
F

fifo

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.
 
D

Dan

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

Dan
 
K

Keith Keller

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


[TOFU snipped]
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 address 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)

iD8DBQFA7u1ghVcNCxZ5ID8RApzjAJ9bfWxhNnQXnvhuZ8i85rQ6HAKlZwCgi74k
I1Bk5YENJOfknoO4mObD9vE=
=3cj7
-----END PGP SIGNATURE-----
 
T

Tad McClellan

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


Dan said:
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 ]
 
D

Dan

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 said:
[ Please learn (and then use) the proper form for a followup posting ]


Dan said:
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 ]
 
T

Tad McClellan

[ 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! ]
 
D

Dan

Tad McClellan said:
[ 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
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

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
 

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

Similar Threads

RegEx 0
REGEX Negation 2
Help with dynamic regex 14
Negation of RegEx 3
regex puzzle 8
Regexp: Negation with backreference? 6
My regex kung-fu is not strong =( 0
Simple pattern matching negation 8

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top