Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

replace characters in link

by ytjPerl (Scribe)
on Dec 11, 2018 at 15:28 UTC ( [id://1227100]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I basically know how to replace partial of string with designated characters. But I have trouble with the string containing special characters which pass to perl variable and which I only keep it as it is in perl. For instance, I have a web link as https://abc.abc:8080/abc?abcdef&abc=&egh?, I am intending to replace abc.abc:8080 as def.def and keep the rest of it as it is, so the link would be https://def.def/abc?abcdef&abc=&egh?

Is there a way to do it ?

Thanks!

Replies are listed 'Best First'.
Re: replace characters in link
by haukex (Archbishop) on Dec 11, 2018 at 15:43 UTC

    You could use URI to safely parse and modify those kinds of strings.

    use warnings; use strict; use URI; my $input = 'https://abc.abc:8080/abc?abcdef&abc=&egh?'; my $uri = URI->new($input); $uri->host('def.def'); $uri->port(undef); my $output = "$uri"; use Test::More; my $expect = 'https://def.def/abc?abcdef&abc=&egh?'; is $output, $expect or diag explain $output; done_testing;
      does it need to escape the special character for the input in this case? Actually my input is dynamic.
        does it need to escape the special character for the input in this case? Actually my input is dynamic.

        That depends on what special character you mean? URI should be able to handle just about any supported URI you throw at it. Can you show some more examples of what you mean? In fact, my example code includes Test::More, and you can easily extend it with more test cases to see if it does what you want.

        Also, please be careful, you've reposted your question twice now. If you have the browser open to the submission page, don't reload it.

Re: replace characters in link
by hippo (Bishop) on Dec 11, 2018 at 16:50 UTC
    Is there a way to do it ?

    This is Perl, so TIMTOWTDI. In addition to haukex's approach with URI you could live a bit more dangerously and try any of these:

    use strict; use warnings; use Test::More tests => 3; my $have = 'https://abc.abc:8080/abc?abcdef&abc=&egh?'; my $want = 'https://def.def/abc?abcdef&abc=&egh?'; my $try = $have; substr ($try, 8, 12, 'def.def'); is ($try, $want, 'substr'); ($try = $have) =~ s/\Qabc.abc:8080/def.def/; is ($try, $want, 'quotemeta regex'); my @bits = split ('/', $have, 4); $bits[2] = 'def.def'; $try = join '/', @bits; is ($try, $want, 'split/join');

    There are plenty more too - this is just scratching the surface.

      What does the "is" keyword do in perl? I have been trying to find documentation on it, but I couldn't find anything.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1227100]
Approved by haukex
Front-paged by Corion
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-25 10:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found