Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Negative Lookahead using negated character classes?

by Wonko the sane (Deacon)
on Oct 28, 2003 at 16:50 UTC ( [id://302719]=perlquestion: print w/replies, xml ) Need Help??

Wonko the sane has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

Here is what I am trying to do.

if ( m|/user/(?!prot/)| )
The problem is I cannot use negative lookahead to do it.

To do this, I have tried the following variations with no success.

#!/usr/local/bin/perl5.6.0 -w use strict; while ( <DATA> ) { chomp; #if ( m|/user/(?!prot/)| ) # Works #if ( m|/user/[^p]?[^r]?[^o]?[^t]?[^/]?| ) #if ( m|/user/([^p]*?[^r]*?[^o]*?[^t]*?[^/]*?)?| ) #if ( m|/user/[^p]+?[^r]+?[^o]+?[^t]+?[^/]+?| ) #if ( m|/user/([^p]*[^r]*[^o]*[^t]*[^/]*)?| ) { print qq{Disallow!! [$_]\n}; } else { print qq{Allowed. [$_]\n}; } } __DATA__ /user/prot/ /user/protrusions/ /user/a/ /user/backups
The only one that I want to print "Allowed", (not to match) is the first one "/user/prot/".

OT: Why cant I use negative lookahead?

Actually because I am trying to make a regex for an Apache LocationMatch section,
to disable some settings for all directories inside a users site, except for one,
'/user/prot'.
The regex engine in Apache though does not have lookaround assertion ability :(.

<LocationMatch /user/(?!prot/) > <Limit GET> order allow,deny deny from all </Limit> AllowOverride None </LocationMatch>
Any help is much appreciated.

Wonko

Replies are listed 'Best First'.
Re: Negative Lookahead using negated character classes? (|)
by tye (Sage) on Oct 28, 2003 at 17:07 UTC

    m#/usr/(prot([^/]|$)|pro([^t]|$)|pr([^o]|$)|p([^r]|$)|[^p]|$)#

    (updated)

                    - tye
Re: Negative Lookahead using negated character classes?
by pg (Canon) on Oct 28, 2003 at 17:32 UTC

    regexp is challenging and fun, but not always the best solution, sometime it could be ugly and not maintainable.

    In this case, first split on /, to get the prot part, then use the power of bitwise XOR like this (I choose not to use natural language to descibe, you should be able to understand what I am trying to say):

    use strict; my $str = "paat"; (my $cmp = $str ^ "prot") =~ s/(.)/ord($1) ? "0" : "1"/eg; print $cmp; #prot => 1111 #pro? => 1110 #pr?? => 1100 #p??? => 1000 #???? => 0000
Re: Negative Lookahead using negated character classes?
by delirium (Chaplain) on Oct 28, 2003 at 21:04 UTC
    Not Perl, but why not do something like:

    <LocationMatch /user/> <Limit GET> order allow,deny deny from all </Limit> AllowOverride None </LocationMatch> <LocationMatch /user/prot/> Override all the stuff above </LocationMatch>
Re: Negative Lookahead using negated character classes?
by BrowserUk (Patriarch) on Oct 28, 2003 at 22:22 UTC

    Doesn't this do what you want?

    #! perl -slw use strict; while ( <DATA> ) { chomp; if ( $_ !~ m[/user/prot/$] ) { print qq{Disallow!! [$_]}; } else { print qq{Allowed. [$_]}; } } __DATA__ /user/prot/ /user/protrusions/ /user/a/ /user/backups

    Output

    P:\test>test2 Allowed. [/user/prot/] Disallow!! [/user/protrusions/] Disallow!! [/user/a/] Disallow!! [/user/backups]

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!

Re: Negative Lookahead using negated character classes?
by ptkdb (Monk) on Oct 28, 2003 at 17:21 UTC
    by implication, you seem to be using perl 5.6.0. That's a bit out of date isn't it?
Re: Negative Lookahead using negated character classes?
by Abigail-II (Bishop) on Oct 28, 2003 at 17:10 UTC
    Actually because I am trying to make a regex for an Apache LocationMatch section
    What has that to do with Perl? If everyone is going to ask his or her non-Perl language questions here, it's going to be hard to find the interesting questions.

    Abigail

      Because we use apache to render our perl apps. So stop being a sniveling little biotch. BTW is this the same abigail that gave the boring, emotionless, half ass tutorial at the last YAPC at F.A.U?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://302719]
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found