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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: regex help
by sauoq (Abbot) on Sep 27, 2002 at 20:44 UTC

    Your question is poorly stated. If you always want "/ASP/scripts/homesites/" then you could just set a variable to that. You wouldn't need a regex. So, I suspect that you want to match a certain class of strings.

    That could be a class of strings that always consists of the 2nd through 3rd directories in the path component of a URI. That could be a class of strings that consists of "/ASP" followed by two more directories. etc. etc. etc.

    What do you really want to match? Can you provide more examples?

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: regex help
by hiseldl (Priest) on Sep 27, 2002 at 20:41 UTC

    Update: added quote.

      I just wanna match everything after browse and everything before the last "/"

    $a =~ s#browse(.*/)#$1#; Because .* is greedy it will match everything up to the last '/'.

    --
    hiseldl
    What time is it? It's Camel Time!

Re: regex help
by Cody Pendant (Prior) on Sep 28, 2002 at 08:54 UTC
    As an aside, try not to use $a, because it's a special variable used for sorting (comparing $a against $b).
    --
    ($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;
Re: regex help
by fglock (Vicar) on Sep 27, 2002 at 20:40 UTC
    s|/ASP/scripts/homesites/||;

    Is this what you mean?

      I cnat use that cause I have other ones that are like "/browse/CGI/scripts/" that are totally different
Re: regex help
by andrew (Acolyte) on Sep 27, 2002 at 21:24 UTC
    Alright

    $a =~ s|/browse(/.*/)|$1|i; $a = $1;

    That seems to work :), but what if the url conatins '\' instead of '/' like how could I check for both.

      '\' or '/' is written like tihs:

      [\\\/] or: [\/\\] so you get: $a =~ s|[\/\\]browse([\/\\].*[\/\\])|$1|i;
Re: regex help
by andrew (Acolyte) on Sep 27, 2002 at 20:47 UTC
    I just wanna match everything after browse and everything before the last "/"
      Try this:
      while ( <DATA> ) { if ( /browse(\/.*)\// ) { #if the current $_ matches #browse followed by a # / followed by anything #but having to be followed by # another / #then put whatever was found #between the parens in $line my $line = $1; print "$line\n"; } } __DATA__ http://www.url.com/browse/ASP/scripts/homesites/ /browse/ASP/scripts/homesites/ /browse/ASP/scripts/homesites/index.htm
      I don't know if you noticed but in your original post browse was misspelled. -Enlil
        while ( <DATA> ) { if ( /browse(\/.*\/)/ ) { #if the current $_ matches #browse followed by a # / followed by anything #but having to be followed by # another / #then put whatever was found #between the parens in $line my $line = $1; print "$line\n"; } } __DATA__ http://www.url.com/browse/ASP/scripts/homesites/ /browse/ASP/scripts/homesites/ /browse/ASP/scripts/homesites/index.htm
        I meant to put the last \/ inside the parens as you wanted the final / included in the match at the end of the returned string. My mistake.

        -Enlil

      my ($match) = $a =~ m!(/browse.*/)!;

      Use that and $match will contain the slash prior to "browse" and everything following up to and including the final slash.

      Be forewarned, that will break if you give it a string ending in a directory that you want to match but which doesn't have a slash. The string 'http://foo.bar.com/some/path/browse/ASP/scripts', for example, would cause $match to be set to '/browse/ASP/'. If that's alright, then great. If not, they you have to find another way of distinguising a file and a directory. (Perhaps all of your files contain a dot and none of your directories do.)

      -sauoq
      "My two cents aren't worth a dime.";
      
      Well, everything after browse and everything before the last "/"
      can be also like this
      $a =~ s|/browse(.*/)|$1|i;
      Comment: Ooops! Too late! It's like hiseldl

      Hopes
      perl -le '$_=$,=q,\,@4O,,s,^$,$\,,s,s,^,b9,s,$_^=q,$\^-]!,,print'
A reply falls below the community's threshold of quality. You may see it by logging in.