in reply to Detecting whether forward slash is present in string

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.

Replies are listed 'Best First'.
Re^2: Detecting whether forward slash is present in string
by davido (Cardinal) on Sep 14, 2004 at 08:24 UTC

    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

    Dave