This solution is similar to kennethk's but uses a look-ahead in the regular expression and pos to find where the match occurred rather than special variables.

use strict; use warnings; use 5.010; my @searches = qw{ .LLL LLLL .RRR RRRR }; chomp( my @lines = <DATA> ); foreach my $search ( @searches ) { my $rxSearch = qr{(?=\Q$search\E)}; my $length = length $search; foreach my $line ( @lines ) { my ( $string, $mask ) = split m{\s+}, $line, 2; say qq{Looking for '$search' in '$mask'}; while ( $mask =~ m{$rxSearch}g ) { my $pos = pos $mask; my $ind = q{ } x length $mask; substr $ind, $pos, $length, q{^} x $length; say qq{ Found at offset $pos}; say qq{ $string\n $mask}; say qq{ $ind - @{ [ substr $string, $pos, $length ] }} +; } } } __DATA__ THAETURQU .LLL..RRR JURYATAUTIOPW .LLL...LL.RRR TURIWOWQNQQUTYRIOPWMNHF L..L..LLLL.LL.R..L.LLLL

The output.

Looking for '.LLL' in '.LLL..RRR' Found at offset 0 THAETURQU .LLL..RRR ^^^^ - THAE Looking for '.LLL' in '.LLL...LL.RRR' Found at offset 0 JURYATAUTIOPW .LLL...LL.RRR ^^^^ - JURY Looking for '.LLL' in 'L..L..LLLL.LL.R..L.LLLL' Found at offset 5 TURIWOWQNQQUTYRIOPWMNHF L..L..LLLL.LL.R..L.LLLL ^^^^ - OWQN Found at offset 18 TURIWOWQNQQUTYRIOPWMNHF L..L..LLLL.LL.R..L.LLLL ^^^^ - WMNH Looking for 'LLLL' in '.LLL..RRR' Looking for 'LLLL' in '.LLL...LL.RRR' Looking for 'LLLL' in 'L..L..LLLL.LL.R..L.LLLL' Found at offset 6 TURIWOWQNQQUTYRIOPWMNHF L..L..LLLL.LL.R..L.LLLL ^^^^ - WQNQ Found at offset 19 TURIWOWQNQQUTYRIOPWMNHF L..L..LLLL.LL.R..L.LLLL ^^^^ - MNHF Looking for '.RRR' in '.LLL..RRR' Found at offset 5 THAETURQU .LLL..RRR ^^^^ - URQU Looking for '.RRR' in '.LLL...LL.RRR' Found at offset 9 JURYATAUTIOPW .LLL...LL.RRR ^^^^ - IOPW Looking for '.RRR' in 'L..L..LLLL.LL.R..L.LLLL' Looking for 'RRRR' in '.LLL..RRR' Looking for 'RRRR' in '.LLL...LL.RRR' Looking for 'RRRR' in 'L..L..LLLL.LL.R..L.LLLL'

I hope this is useful.

Cheers,

JohnGG

Update: Corrected typo.


In reply to Re: Extracting string based on comparison to second string? by johngg
in thread Extracting string based on comparison to second string? by trudy123g

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.