Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > REGEX Negation

Reply
Thread Tools

REGEX Negation

 
 
Rusty Phillips
Guest
Posts: n/a
 
      06-23-2004
I know about negative lookahead and negative character closures,
but I can't find any good way to do actual negation.

One thing I'd like to use this for is to match quotes while
guaranteeing that I'm not matching backslashed quotes (that is, if I
find a backslash in the string, the quote in front of it should not
be matched).
This string:
String = q{She said,
Welcome to the party\\""}

Should match
He said, \\"Welcome to the party\\"
as the part within the quotes, and not
He said, \\

There are many more places where I'd like to use a negation
technique - especially I'd like to match things of the form:
"match the largest string that doesn't contain the character sequence
'blah.'"

Are there any ways to do either of these types of negation?
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      06-24-2004
Rusty Phillips <(E-Mail Removed)> writes:

> I know about negative lookahead and negative character closures,
> but I can't find any good way to do actual negation.


There is in general no way to do negation in regex.

> One thing I'd like to use this for is to match quotes while
> guaranteeing that I'm not matching backslashed quotes (that is, if I
> find a backslash in the string, the quote in front of it should not
> be matched).


You are talking about negative lookbehind. This is documented not far
from where negative lookahead is documented.

However, one usually looks for an even number of backslahes followed
by a quote. (Note: zero is an even number).

/(<?!\\)(?:\\\\)*"/

Another approach not using lookbehind is given in the answer to the
FAQ "How can I split a [character] delimited string except when inside
[character]? (Comma-separated files)"

Not, of course, that you could have been expected to guess that
because yours not really the same question but is in fact the _next_
question people usually ask after asking the one in the FAQ.

> There are many more places where I'd like to use a negation
> technique -


Sorry, you have to refactor your question so that is does not invlove
negation.

> especially I'd like to match things of the form:
> "match the largest string that doesn't contain the character sequence
> 'blah.'"


Regex can never find the longest - it will find always the first (or
occasionally the last). Within matches starting at the same position
it can be made to favour long or short. So to get the globally
longest match you need to find all such strings and sort.

These strings will be the same set as the set of shortest strings to
start at the beginning of the input or at the 'l' of 'blah' and to end
at the end of the input or at the 'a' of 'blah'

my @substrings = /(?=((?:^|(?<=b)lah).*?(?:$|bla(?=h))))/g;

For example for $_='xxxxblablahwibbleblahfoo' this gives @substrings =
('xxxxblabla','lahwibblebla','lahfoo').

You can then find the longest with sort() or List::Util::reduce().

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
Reply With Quote
 
 
 
 
Rusty Phillips
Guest
Posts: n/a
 
      06-24-2004
That second technique is how I'm doing things now for the one
"negation" I'm doing now (actually /(.*?)(?=blah|$)/, which I know
will at least find the first match not containing the lookahead
string (assuming that such a string is a token, and should not be
absorbed by the regex). I'd just hoped there was a more natural way.

Didn't consider negative lookbehinds for doing quotes, though.
Thanks for the help.
 
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
Negation of RegEx Dan Perl Misc 11 07-12-2004 01:42 PM
Negation of RegEx Andreas Hochsteger Perl Misc 3 04-21-2004 09:11 PM



Advertisments