Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Detecting whether forward slash is present in string

by philuk86zen (Initiate)
on Sep 13, 2004 at 21:48 UTC ( [id://390670]=perlquestion: print w/replies, xml ) Need Help??

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

hi im trying to combine strings from a database to form a url, the problem is some strings end with a "/" and some start with a "/". eg string1 - string2 http://www.somecoolwebsite.com/ /images/index.html as you can see when i add them together they form http://www.somecoolwebsite.com//images/index.html I need to somehow remove the leading slash from the second string if there is a trailing slash on the first string. any help is appreciated.
  • Comment on Detecting whether forward slash is present in string

Replies are listed 'Best First'.
Re: Detecting whether forward slash is present in string
by bart (Canon) on Sep 13, 2004 at 22:39 UTC
    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.

      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

Re: Detecting whether forward slash is present in string
by zdog (Priest) on Sep 13, 2004 at 21:54 UTC

    Good ol' substitution regex:

    # 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

Re: Detecting whether forward slash is present in string
by Happy-the-monk (Canon) on Sep 13, 2004 at 21:53 UTC

    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

Re: Detecting whether forward slash is present in string
by Limbic~Region (Chancellor) on Sep 13, 2004 at 22:10 UTC
    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;

    Cheers - L~R

    I am trying to avoid regexes today as noted by this node
      chop reverse doesn't work; try $beg = substr($string2,0,1,'');  (Untested, of course :)
        ysth,
        I am glad I put the should also work disclaimer in there ;-)

        You are of course correct. Lesson learned - always state your code is untested if you are rushing off to have dinner with your wife.

        Cheers - L~R

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://390670]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-24 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found