negation - RE

F

fred78980

Case1
This is my RE to negate cer which is OK
negation of (cer)
(\w{0,10})(?<!c)er
This take every word ending with er except word that end cer

Case2
and I use a similar RE to negate bcer
negation of (bcer)
(\w{0,10})(?<!b)(?<!c)er
I tried (?<!bc) which is not working

Case 3
negation of (b|c|d)er
(\w{0,10})(?<!b)(?<!c)(?<!d)er
This RE is too long

Please help me with case 2 and 3.

Many thanks
 
B

Ben Morrow

Quoth (e-mail address removed):
Case1
This is my RE to negate cer which is OK

You will need to explain what you mean by 'negate'. It seems you have
some function which takes a regex as input and attempts to make a new
regex that matches some different set of data? You will need to explain
what forms of regex are possible input, and how you determine what you
want the output regex to match. The best way to do this is to post a
*short* example program *with input data* that demonstrates your
problem.
negation of (cer)
(\w{0,10})(?<!c)er
This take every word ending with er except word that end cer

OK, though it would probably be simpler to write it as

/ ( \w{0,9} [^\Wc] ) er /x

i.e. matching what you want to match, rather than matching a \w and then
making a look-behind assertion about it.
Case2
and I use a similar RE to negate bcer
negation of (bcer)
(\w{0,10})(?<!b)(?<!c)er
I tried (?<!bc) which is not working

I think you are confused about how look-around assertions work. (It is
rather confusing.) They do not consume any of the matched string, so
/(?<!b)(?<!c)/ is equivalent to /(?<! b|c)/x rather than to /(?<!bc)/.
If what you want is 'twelve-character words ending in "er" but not in
"bcer"', then it is the latter you want; since you say it is not
working, you will have to say what you expected it to match.

This is where look-behinds really are valuable. Matching something like
that 'by construction', while possible, is terribly messy.
Case 3
negation of (b|c|d)er
(\w{0,10})(?<!b)(?<!c)(?<!d)er
This RE is too long

Huh? Too long for what? Perl is quite happy with it.

Ben
 

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
474,206
Messages
2,571,069
Members
47,674
Latest member
scazeho

Latest Threads

Top