Hello all,

(first post at PerlMonks)

I have a regular expression to find sequence patterns in an alphanumeric string ($seq) and I would like to find out if it is possible (and how to do it) to track the positions (counting at zero at the first character of the string) of the pattern matches.

For example, if

$seq = "HHHHHTHHHHH55HHHH5H"

my REGEXP will find any number of 5's surrounded by any number of H's. In this case it will find a match at position 12 and one at position 18. But how can automate the task so I can get the position? I.e. I need the program to count from the beginning and every time it finds one (typically more than once), it will write out the positions of each one (in the order found).

My code is:
#!/usr/bin/perl -w # use strict; my $count = (); my $seq = " TTHTT SHHHHHHHHH55HHHHTT HHTHHHH5HHHHH "; while ($seq =~/H{1,}5{1,}H{1,}/g) { $count++; } print "Sequence: $seq\n"; print "Count: $count\n"; exit;

Thank you for reading and helping!

JP

In reply to Pattern Matching with REGEXP: Is the match position trackable? by luthor

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.