If an URL starts with a slash, it's supposed to be an absolute URL, using the same domain as your reference domain.
That aside, I would think of just concatenate the string, and turn double (or triple) slashes into single ones. The only problem with that approach is that the double slash at the start, right after the colon, should stay. so a plain
tr[/][]s
will not do.
This will work:
(my $url = "$string1/$string2")
=~ s((://)|//+){ $1 || "/" }ge;
After that, the resulting string will be in $url. It will not be the return value of the entire expression, don't make the mastake of returning it directly in a function.
n.b. I'm inserting a slash myself, in case neither of the two strings contained a slash at their appropriate end. | [reply] [d/l] [select] |
I agree that a solution where you start by just concatenating the strings, and then deal with duplicate // characters later is probably a fairly simple approach. Here's a solution similar to yours but using a negative lookbehind assertion to preserve the // if it follows a word character and a colon. That should prevent "http://" from becoming "http:/" (just as your solution also prevents this problem).
One issue with both of our solutions is that "file:///...." would be changed to "file://".
Here it is...
use strict;
use warnings;
while ( <DATA> ) {
chomp;
next unless $_;
print "$_\t=>\n";
s{(?<!\w:)//}[/]g;
print "$_\n\n";
}
__DATA__
http://www.perlmonks.org/index.html
http://www.perlmonks.org//index.html
http:///davido.perlmonk.org/index.html
http://www.yahoo.com:8080/index.html
| [reply] [d/l] |
# if ( substr( $string1, -1, 1 ) eq "/" ) # check for trailing slash.
if ( $string1 =~ s|/\s*$|/| ) # even better
{
$string2 =~ s|^/||; # remove leading slash if it's present.
}
Update: Added additional code to satisfy problem requirements more fully.
Zenon Zabinski | zdog | zdog@perlmonk.org
| [reply] [d/l] |
Have you tried a Regex to find out if either string1 ends or string2 begins with a slash?
Or substringed the last and first characters and done an eq comparison?
Cheers, Sören
| [reply] |
philuk86zen,
I would think URI would be of some help here, but the following code should also work:
my $end = chop $string1;
my $beg = chop reverse $string2;
my $url = $string1 . ($end eq '/' ? '' : $end) . '/' . ($beg eq '/' ?
+'' : $beg) . $string2;
I am trying to avoid regexes today as noted by this node | [reply] [d/l] |
chop reverse doesn't work; try $beg = substr($string2,0,1,''); (Untested, of course :)
| [reply] [d/l] |
| [reply] [d/l] |