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

my $string = "DSI+DSE+"; if($string =~m/DSI+DSE/) { print "yes\n"; } my first question is why doesn't it print yes since DSI+DSE is found in string?? and then how can you find the length of overlap??..in this case its 7

Replies are listed 'Best First'.
Re: string problem
by johngg (Canon) on Mar 05, 2010 at 18:07 UTC

    Because the plus sign is a regular expression metacharacter and must be escaped.

    $ perl -E ' > $str = q{DSI+DSI+}; > say q{Yes} if $str =~ m{(DSI\+DSI)}; > say qq{Matched $1, length @{ [ length $1 ] }};' Yes Matched DSI+DSI, length 7

    I hope this is helpful.

    Cheers,

    JohnGG

Re: string problem
by Corion (Patriarch) on Mar 05, 2010 at 18:02 UTC

    See perlre on the regex meta characters and potentially quotemeta.

    If you know the length of your fixed size target match, the size of the overlap will always be the length of your fixed size target match. If this some school homework, you want to read perlre again, and perlvar on the match variables.