http://www.perlmonks.org?node_id=717447

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


Hi Monks

Suppose my $qname="_sip._udp.www.google.com"; Can anyone suggest me a suitable function, so that i can extract the "_sip._udp." from the variable $qname and store the rest part(i.e www.google.com) in a variable $qname1.

Regd's
Sanjay
  • Comment on How can i get a substring from a String?

Replies are listed 'Best First'.
Re: How can i get a substring from a String?
by JavaFan (Canon) on Oct 16, 2008 at 11:20 UTC
    Assuming "_sip._udp." is a fixed string, substr comes into mind:
    my $qname = "_sip._udp.www.google.com"; my $qname1 = substr $qname, 10;
Re: How can i get a substring from a String?
by lamp (Chaplain) on Oct 16, 2008 at 11:40 UTC
    Use Regex.
    $qname="_sip._udp.www.google.com"; $qname =~ m/(_sip\._udp\.)(.*)/; $qname1 = $2;
    If you want to replace string "_sip._udp." from the variable, you can use the following code.
    $qname="_sip._udp.www.google.com"; $qname =~ s/(_sip\._udp\.)(.*)/$2/;
Re: How can i get a substring from a String?
by krusty (Hermit) on Oct 16, 2008 at 11:56 UTC
    The solution proposed with substr will work, but only if the preceding characters before www.google.com are always 10 characters long. If that is always the case, then substr will work well. If the character length preceding your URL is variable, may I suggest split and join?
    $qname1 = join ".",(split(/\./, $string))[2,3,4];

    The code will only work if there are exactly two dot separated strings prior to the url. If that, too, is variable, then perhaps you could try this small modification:
    $qname1 = join ".",(split(/\./, $string))[-3,-2,-1];

    Cheers,
    Kristina
      #!/usr/bin/perl -- # use strict; use warnings; my $qname = "_sip._udp.www.google.com"; warn substr $qname, index $qname, 'www'; warn substr $qname, length('_udp.') + index $qname, '_udp.'; __END__ www.google.com at substr.index.pl line 9. www.google.com at substr.index.pl line 10.
Re: How can i get a substring from a String?
by Bloodnok (Vicar) on Oct 16, 2008 at 11:46 UTC
    my ($qname = "_sip._udp.www.google.com") =~ s/(.*\.)(www.*)/; ($qname, my $qname1) = ($1, $2);

    A user level that continues to overstate my experience :-))
      Close... but not quite:
      use strict; use warnings; # from bloodnok's reply to 717447 # my ($qname = "_sip._udp.www.google.com") =~ s/(.*\.)(www.*)/; # typo +? Not substitution, which throws an error # ($qname, my $qname1) = ($1, $2); # error: Can't declare scalar assignment in "my" at 717447.pl line 9, +near ") =~" my $qname; $qname = ("_sip._udp.www.google.com") =~ m/(.*\.)(www.*)/; print "\$1 is: $1 \n \$2 is: $2\n";

      Output

      $1 is: _sip._udp. $2 is: www.google.com

      Update: corrected to clarify initial commented line which has two problems

        Bugger!!!

        TFT ww, that oughta teach me not to post from a perl-less environment !!!

        .oO(I knew my sig wasn't in vain)

        A user level that continues to overstate my experience :-))