Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Negated Perl Regexp

Reply
Thread Tools

Negated Perl Regexp

 
 
Mumia W.
Guest
Posts: n/a
 
      05-31-2006
Xicheng Jia wrote:
> [...]
> you want to match anything except those matching the qr//
> expression???? so you might want to try the following:
>
> my $RE = qr/something here/;
>
> if ($v =~ /^(??!$RE).)*$/) {
> # any string $v that doesnot match $RE
> }
>
> (untested)
> Xicheng
>


Well, I tested it, and it seems pretty darn good, and just like Ronny, I
might end up using this in my programs if I can figure out how it works.
Thanks Xicheng.
 
Reply With Quote
 
 
 
 
Ted Zlatanov
Guest
Posts: n/a
 
      05-31-2006
On 31 May 2006, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> You exactly got the point: I want the user to rewrite the
> Pattern. The question is, how to write a *negated* pattern using
> Perl RE Syntax?


You can do it for some cases, but because of limitations on memory and
CPU cycles, most complex regexes can't be inverted in a reasonable
amount of time. When there's code inside, it gets even worse.

Look at the book "Higher-Order Perl" by Mark-Jason Dominus. It has a
long section on finding all the strings that can match a given regular
expression; if you read it carefully you'll see why inverting a
regular expression is generally a hard problem, just as producing all
the strings that match it.

Note also that if security is a concern, giving users regexp access is
equivalent to letting them run any code due to the code escapes
possible in Perl's regex interpreter. It may be simpler to give the
users a limited language with a NOT operator. Parse::RecDescent has
some good examples of this kind of parser in the distribution. The
users may also prefer this to the raw power of regexps, and it's what
I would do for a production system.

> Of course, one hack for my original problem would be to "invent" a
> special character (say, exclamation mark) which is allowed to be at
> the very start of the expession, and just has the meaning "pattern
> has negated meaning".


Yes That would be easiest.

>> I honestly don't see a reason why you shouldn't provide a -v option,

>
> The reason is because I simplified the problem very much so to make
> it better feasible to discuss here. The interesting point for me is
> not finding out whether the negation effect can be done solely
> within the pattern, or has to be "moved outside" to the distinction
> between =~ and !~, or if/unless construct.


It should be moved outside, so you can go on to finish the project

Ted
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      05-31-2006

Xicheng Jia wrote:

> my $RE = qr/something here/;
>
> if ($v =~ /^(??!$RE).)*$/) {
> # any string $v that doesnot match $RE
> }
>


I've not benchmarked it but I'd suspect that's less efficient than the
usual answer[1] the OP would have found if he'd been bothered to type
"negate regex" into a Usenet search engine on this newsgroup.

[1] The on ska gave.

 
Reply With Quote
 
Xicheng Jia
Guest
Posts: n/a
 
      05-31-2006
Brian McCauley wrote:
> Xicheng Jia wrote:
>
> > my $RE = qr/something here/;
> >
> > if ($v =~ /^(??!$RE).)*$/) {
> > # any string $v that doesnot match $RE
> > }
> >

>
> I've not benchmarked it but I'd suspect that's less efficient than the
> usual answer[1] the OP would have found if he'd been bothered to type
> "negate regex" into a Usenet search engine on this newsgroup.


Here is an old post from Tom Christensen which might best address this
problem:

http://groups.google.com/group/comp....f7898218075b5b

while the notion of (??!$RE).)* to match anything except $RE(as far
as I can know) is from Jeffery's book "Mastering Regular Expression".

HTH,
Xicheng

 
Reply With Quote
 
Ronny
Guest
Posts: n/a
 
      06-01-2006

> I've not benchmarked it but I'd suspect that's less efficient than the
> usual answer[1] the OP would have found if he'd been bothered to type
> "negate regex" into a Usenet search engine on this newsgroup.


Point taken!

Ronald

 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      06-01-2006
On 31 May 2006, (E-Mail Removed) wrote:

Brian McCauley wrote: > Xicheng Jia wrote: >
>>> my $RE = qr/something here/;
>>>
>>> if ($v =~ /^(??!$RE).)*$/) {
>>> # any string $v that doesnot match $RE
>>> }
>>>

>>
>> I've not benchmarked it but I'd suspect that's less efficient than the
>> usual answer[1] the OP would have found if he'd been bothered to type
>> "negate regex" into a Usenet search engine on this newsgroup.

>
> Here is an old post from Tom Christensen which might best address this
> problem:
>
> http://groups.google.com/group/comp....f7898218075b5b
>
> while the notion of (??!$RE).)* to match anything except $RE(as far
> as I can know) is from Jeffery's book "Mastering Regular Expression".


This post does not mention that negating some regexes is
computationally prohibitive, and code escapes are a problem. Also,
the "Higher-Order Perl" book I mentioned came out after that post
(1999), and has some very interesting information in the chapter on
generating all the possible strings a regex can match. There's
security considerations when you allow a user to provide you with a
regex. None of those things is answered by a naive Usenet search.

Furthermore, the real question was "why doesn't the OP want a -v flag?
How can he simulate it instead?" and not "how to negate a regex."
Usually that's the case when people ask for negating a regex, btw.

Ted
 
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
Calculating a negated character class Klaus Perl Misc 2 06-18-2012 04:55 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Regular Expressions: "Negated Strings" instead of "Negated Character Classes" lmeurs@gmail.com Perl Misc 6 06-08-2007 04:32 PM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM



Advertisments