Regex help for URL

Discussion in 'Perl Misc' started by Seb, Oct 7, 2006.

  1. Seb

    Seb Guest

    Seb, Oct 7, 2006
    #1
    1. Advertising

  2. Seb

    Ben Morrow Guest

    Quoth "Seb" <>:
    >
    > I am trying to find the right regular expression which would only
    > validate a URL with a given number of folders.
    >
    > Example:
    >
    > http://www.abc.com/folder/page.htm --> Valid (4 slahes)
    >
    > http://www.abc.com/folder/subfolder/ --> not valid (5 slashes)
    >
    > Basically, any URL not made of 4 slahes would be invalid.
    > However, the URL:
    >
    > http://www.abc.com/folder/subfolder --> would also be invalid


    Please explain how anyone is supposed to tell from looking at that URL
    that the last /subfolder is a directory ('folder') and not a file.

    Please explain how anyone is supposed to tell by any means whatever,
    given that http (setting aside WebDAV) has no concept of 'directory';
    other that by guessing that if retrieving it gives a redirect to
    http://.../subfolder/ then the URL refers to some directory in some
    filesystem somewhere.

    A regex to match a string with exactly four slashes in it is

    m{ ^ (?: [^/]* / ){4} [^/]* $ }x

    , but I do not think that will help you here.

    Ben

    --
    Although few may originate a policy, we are all able to judge it.
    Pericles of Athens, c.430 B.C.
    Ben Morrow, Oct 7, 2006
    #2
    1. Advertising

  3. Seb

    -berlin.de Guest

    Seb <> wrote in comp.lang.perl.misc:
    > Hi,
    >
    > I am trying to find the right regular expression which would only
    > validate a URL with a given number of folders.
    >
    > Example:
    >
    > http://www.abc.com/folder/page.htm --> Valid (4 slahes)
    >
    > http://www.abc.com/folder/subfolder/ --> not valid (5 slashes)
    >
    > Basically, any URL not made of 4 slahes would be invalid.
    > However, the URL:
    >
    > http://www.abc.com/folder/subfolder --> would also be invalid
    >
    > Any ideas?


    What have you tried?

    We can help you fix your code, but we rarely deliver solutions
    to specification.

    Anno
    -berlin.de, Oct 7, 2006
    #3
  4. Seb

    Guest

    Seb wrote:
    > http://www.abc.com/folder/page.htm --> Valid (4 slahes)
    > http://www.abc.com/folder/subfolder/ --> not valid (5 slashes)


    perldoc -q count
    How can I count the number of occurrences of a substring within a
    string?

    > http://www.abc.com/folder/subfolder --> would also be invalid


    That's arbitrary. The client has no way to tell if "subfolder" is a
    directory (where a default DirectoryIndex document presumably exists)
    or a document/file named "subfolder". Only the server knows for sure.

    --
    David Filmer (http://DavidFilmer.com)
    , Oct 7, 2006
    #4
  5. Seb <> wrote:

    > I am trying to find the right regular expression which would only
    > validate a URL with a given number of folders.



    > Basically, any URL not made of 4 slahes would be invalid.



    A regular expression is not the Right Tool for counting characters,
    tr/// is better for that (tr does NOT use any regular expressions).

    print "$url is invalid\n" unless $url =~ tr#/## == 4; # untested


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 7, 2006
    #5
  6. Seb

    Seb Guest

    Thanks. I do need to use a regular expression since it is the only
    mechanism I can use in the search engine I am using to enter a valid
    exclusion.


    Tad McClellan wrote:
    > Seb <> wrote:
    >
    > > I am trying to find the right regular expression which would only
    > > validate a URL with a given number of folders.

    >
    >
    > > Basically, any URL not made of 4 slahes would be invalid.

    >
    >
    > A regular expression is not the Right Tool for counting characters,
    > tr/// is better for that (tr does NOT use any regular expressions).
    >
    > print "$url is invalid\n" unless $url =~ tr#/## == 4; # untested
    >
    >
    > --
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas
    Seb, Oct 7, 2006
    #6
  7. Seb <> wrote:

    > Thanks. I do need to use a regular expression since it is the only
    > mechanism I can use in the search engine I am using to enter a valid
    > exclusion.



    Ask a question in the Perl newsgroup, get an answer in the Perl language.


    [ snip TOFU ]

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 7, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon paugh
    Replies:
    1
    Views:
    703
  2. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    699
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  3. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,622
    Ant...
    Nov 6, 2003
  4. Replies:
    3
    Views:
    757
    Reedick, Andrew
    Jul 1, 2008
  5. Just D.
    Replies:
    0
    Views:
    417
    Just D.
    Aug 11, 2004
Loading...

Share This Page