All pastes #394729 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-03-14 15:15 UTC
rendered paste body
#!/usr/bin/perl

$url = 'http://test.com/blah/doot/stuff.html';

######
# first method of finding url after last /  (the filename)
#
# ^ = beginning of string
# $ = end of string
# . = any character
# * = zero or more matches of the previous character
#
# so .* will match 0 or more of any character (.)
#
# \/ is an escaped or literal /
#
# () anything inside parenthesis, if matched, is returned in the positional
#   arguments
#     $1 for the first set of parentheses
#     $2 for the second set of parentheses
#     etc
#
#  ? means non-greedy ... which is a little weird
#    regexs are, by default greedy, meaning they will match as much as they
#    can.  non-greedy will match as little as they can.  we can get into that
#    more later

# so to summarize what this regex is doing, its going to make sure that it
# matches the whole string by framing it with /^$/;
# its going to do a greedy match (.*) of anything before a literal /
#   which , since its greedy will match multiple literal slashes up until
#   the last one.  note: if it was non-greedy (.*?), it would only match
#   up until one literal /
# so after that literal slash, it starts saving the match with parentheses
#   inside the parentheses, it'll do a non-greedy match of anything (.*?)
#   so that'll get the rest of the line up until the end ($)
if($url =~ /^.*\/(.*?)$/) {
  $filename = $1;
  print "match found filename = \"$filename\"\n";
} else {
  print "no match for filename\n";
}

######
# alternative method of finding url after last /
@tmp = split '/',$url;
$num = $#tmp;
$filename2 = $tmp[$num];
print "filename2 = \"$filename2\"\n";

######
# method of finding base url before and including last / (without the filename)
if($url =~ /^(.*\/).*?$/) {
  $base = $1;
  print "match found base = \"$base\"\n";
} else {
  print "no match for base\n";
}