Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Perl Substr

by lddzjwwy (Novice)
on Jun 17, 2013 at 08:56 UTC ( [id://1039298]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks, now I got an unknown string $s1 and an other string $s2, and I want to check whether $s2 is contained in $s1 and if yes, all characters after this $s2 in $s1 must be returned. How can I realize this with substr()? Thanks a lot guys.

Replies are listed 'Best First'.
Re: Perl Substr
by LanX (Saint) on Jun 17, 2013 at 09:03 UTC
    • use index to find the start of $s2 in $s1
    • add the length to the start position
    • apply substr

    and please try to format your posts

    Use: <p> text here (a paragraph) </p> and: <code> code here </code>

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: Perl Substr
by marto (Cardinal) on Jun 17, 2013 at 09:20 UTC

    You should read up on index, length and substr:

    #!/usr/bin/perl use strict; use warnings; # Example strings my $string1 = "ABCDEFderp12345"; my $string2 = "derp"; # Find position of string2 within string1 my $strpos = index($string1, $string2); # If $strpos is greater than zero (see perldoc index) if ( $strpos >= 0 ){ # Print remainder of the string # See perldoc length, perldoc substr print substr( $string1, ( $strpos + length($string2) ) ); }else{ print "$string2 does not occur within $string1"; }

    In the above basic example above we find out if $string2 exists within $string1. If $string2 does not exist within $string1 then index will return -1, otherwise it'll return the position where $string2 occurs. substr can be used to get the text from $string1 after $string2, but we need to add the length of $string2 to the position returned by index as it's offset, because you don't want to include $string2 in what gets printed.

    Further recommended reading:

    Update: changed gt 0 to >= 0. I wasn't quite awake when I posted. Thanks AnomalousMonk for pointing this out.

    Update 2: Slight rewording for clarity.

Re: Perl Substr
by rovf (Priest) on Jun 17, 2013 at 09:00 UTC
    index tells you the position of $s2 in $s1. length tells you the length of $s2, which you need for telling substr where to start the extraction.

    With

    perldoc -f index
    etc., you get a description of these functions.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: Perl Substr
by BillKSmith (Monsignor) on Jun 17, 2013 at 11:13 UTC
    Why do you exclude a simple regexp? Marte' example becomes:
    use strict; use warnings; my $string1 = "ABCDEFderp12345"; my $string2 = "derp"; if ($string1 =~ m/$string2(.*)$/ ){ print $1, "\n"; }else{ print "$string2 does not occur within $string1"; }
    Bill

      Better to meta-quote  $s2 to avoid strange effects from regex metacharacters in it ( //p regex modifier available with 5.10+):

      >perl -wMstrict -le "my $s1 = 'foo not *UN*known but bar is'; my $s2 = '*UN*known'; ;; die qq{'$s2' not in '$s1'} unless $s1 =~ m{ \Q$s2\E }xmsp; print qq{all after: '${^POSTMATCH}'}; " all after: ' but bar is'
Re: Perl Substr
by Anonymous Monk on Jun 17, 2013 at 09:00 UTC

    Start by trying and showing us what you have tried :) See perlintro, substr

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-20 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found