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

I have the following url:
http://www.foo.com/bar/foobar/bar.html

What i need to do is determine if the user has entered "bar/" or "bar" and not the full URI, "bar/foobar/bar.html"

So far, I have tired this:
if ($string =~ m/bar(\/)?[^.]/) { print "yes\n"; } else { print "No\n"; }

But it always matches "Yes." As far as I can tell the easiest way to do this is match "bar/" or "bar" and reject it if there is anything after the "/", but it doesn't seem to work. My problem is most likely with the negation, but i'm not sure.

Replies are listed 'Best First'.
Re: URL Regex Matching, again
by jeffa (Bishop) on Apr 09, 2004 at 15:08 UTC
    Not exactly what you requested, but this works for this particular case (and should work genericly except for cases like 'http://www.foo.com/bar/bar/')
    #!/usr/bin/perl -l use strict; use warnings; use URI; for ( 'http://www.foo.com/bar/foobar/bar.html', 'http://www.foo.com/bar/foobar/', 'http://www.foo.com/bar/foobar', 'http://www.foo.com/bar/', 'http://www.foo.com/bar', ) { my $uri = URI->new($_); my $path = $uri->path; my @part = split('\/',$path); # use File::Basename here for portab +ility print "$_: ", ($part[-1] eq 'bar') ? 'yes' : 'no'; }

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: URL Regex Matching, again
by Roy Johnson (Monsignor) on Apr 09, 2004 at 16:04 UTC
    Would it be sufficient to determine whether the URL ends in .html (or .htm)?
    print( ($string =~ /.html?$/) ? "Complete\n" : "Incomplete\n" );

    The PerlMonk tr/// Advocate
Re: URL Regex Matching, again
by iburrell (Chaplain) on Apr 09, 2004 at 19:30 UTC
    Where is $string coming from?

    You need anchor the regex to the end of the string to check if the last part of the string is bar optionally followed by a slash.

    $string =~ m|bar/?$/;
    Your regex effectively matches any string containing bar not followed by a dot.