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.


In reply to Re: Perl Substr by marto
in thread Perl Substr by lddzjwwy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.