I was a bit confused about the requirements, but I think this code does it.This code does something. I'm not sure if it does what you want.

#!/usr/bin/perl -w use strict; my @strings = ("CATINTHEHATWITHABAT", "WERWERWAT134wAt"); my $regex = '\wAT'; foreach my $string (@strings) { while ( $string =~ m/($regex)/gi) { printf "%s found at pos %2d in %s\n", $1, pos($string)-length($1)+1, $string; } } __END__ Prints: CAT found at pos 1 in CATINTHEHATWITHABAT HAT found at pos 9 in CATINTHEHATWITHABAT BAT found at pos 17 in CATINTHEHATWITHABAT WAT found at pos 7 in WERWERWAT134wAt wAt found at pos 13 in WERWERWAT134wAt
An update:
#!/usr/bin/perl -w use strict; my @strings = ("CATINTHEHATWITHABAT", "WERWERWAT134wAtThatat", "WERWERWAT134wAtThattat"); my $regex = '\wAT'; foreach my $string (@strings) { while ( $string =~ m/($regex)/gi) { printf "%s found at pos %2d-%-2d in %s\n", $1, pos($string)-length($1)+1, pos($string), $string; } } __END__ Prints: CAT found at pos 1-3 in CATINTHEHATWITHABAT HAT found at pos 9-11 in CATINTHEHATWITHABAT BAT found at pos 17-19 in CATINTHEHATWITHABAT WAT found at pos 7-9 in WERWERWAT134wAtThatat wAt found at pos 13-15 in WERWERWAT134wAtThatat hat found at pos 17-19 in WERWERWAT134wAtThatat WAT found at pos 7-9 in WERWERWAT134wAtThattat wAt found at pos 13-15 in WERWERWAT134wAtThattat hat found at pos 17-19 in WERWERWAT134wAtThattat tat found at pos 20-22 in WERWERWAT134wAtThattat Note: ranges do not "overlap", see "...Thattat"

In reply to Re: Match, Capture and get position of multiple patterns in the same string by Marshall
in thread Match, Capture and get position of multiple patterns in the same string by richardwfrancis

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.