Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Negative expressions with alternation in ProxyRemoteMatch

by redhotpenguin (Deacon)
on Aug 30, 2005 at 05:59 UTC ( [id://487652]=perlquestion: print w/replies, xml ) Need Help??

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

Monks

I'm using ProxyRemoteMatch directive in mod_proxy, and the httpd docs say that Apache's regular expression library is a subset of Perls. I am trying to generate a regular expression to not match a set of words, i.e. not match any expressions that represent images such as "foo.jpg" or "bar.gif".

Normally, I would use negative match with alternation such as !~ m/(some|words)/;, but I can't do that here. My latest attempt has used a zero-width negative lookbehind assertion:

\w+\.(?!gif)(?!jpe?g)$
to not match any urls ending in .gif or .jpe|g that were followed by "imagename" (\w+ in the regex). I've been through the perlre man page, perlretut, but I need to ask for help at this point. I don't know the extent of the subset of Perl's regex engine that is implemented in Apache, but any suggestions here on alternative regex formulations would be most appreciated.

Replies are listed 'Best First'.
Re: Negative expressions with alternation in ProxyRemoteMatch
by Roger (Parson) on Aug 30, 2005 at 06:50 UTC
    Try negative look ahead.

    my @str = qw/ a.jpg b.gif c.png d.txt e.jpeg a.jpg.3 /; for my $str (@str) { if ($str =~ /.*?\.(?!gif|jpe?g)/) { print "matched: $str\n"; } } __END__ # output is matched: c.png matched: d.txt matched: a.jpg.3
      Thanks for the help, this is what I was trying to do. Works great, and it makes sense that the '?!' (whabang?) is at the start of the alternation.
        An alternative is to use negative look behind... Your code was actually negative look ahead. To look behind, you need the < directive.

        /(?<!\.gif)(?<!\.jpg)(?<!\.jpeg)$/
Re: Negative expressions with alternation in ProxyRemoteMatch
by josera (Beadle) on Aug 30, 2005 at 07:14 UTC
    Hi
    I don't know if this could help you, but here is it:
    #!/usr/bin/perl -w use strict; my @string; my $i; $string[0]="http://www.server.com/image.jpg"; $string[1]="http://www.server.com/file.html"; $string[2]="http://www.serverjpg.com/image.jpg"; $string[3]="http://www.serverjpg.com/file.html"; for ($i=0; $i<4; $i++){ if ($string[$i]=~/(.jpg|.gif|.jpeg)$/){ print "Image\n"; } else { print "No image\n"; } }
    The regular expresion is very simple so i hope that it could help you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-19 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found