in reply to regex help

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

Replies are listed 'Best First'.
Re: Re: regex help
by Enlil (Parson) on Sep 27, 2002 at 21:00 UTC
    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

Re: Re: regex help
by sauoq (Abbot) on Sep 27, 2002 at 22:39 UTC
    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.";
    
Re: Re: regex help
by hopes (Friar) on Sep 27, 2002 at 20:58 UTC
    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'