<?xml version="1.0" encoding="windows-1252"?>
<node id="948417" title="Perl Regex Repeating Patterns" created="2012-01-17 16:06:17" updated="2012-01-17 16:06:17">
<type id="115">
perlquestion</type>
<author id="961">
Anonymous Monk</author>
<data>
<field name="doctext">
&lt;p&gt;Dear Monks,&lt;/p&gt;

&lt;p&gt;I have a problem I am trying to solve with regular expressions. I want to return all locations of an array of patterns inside a string. I also only want the patterns to match at locations which are a multiple of 3. For example, so far I have the following.&lt;/p&gt;

&lt;code&gt;my $string="AAABBBCCCCDDDEEEFFFGGGHHHIII";
my @patterns=('BBB','DDD');
my @index;
foreach(@patterns){
while($string=~m/([A-Z]{3})+?$_/g){
push(@index,$-[2]);
pos($string)=$-[2];
}
&lt;/code&gt;

&lt;p&gt;The problem with this is that it matches the first pattern correctly, but it will also match 'CCCDDD' in the second loop because I cannot/dont know how to anchor the pattern to start at a specific location.&lt;/p&gt;

&lt;p&gt;To be clear, I only want the patterns in the array to match if there are a number of characters evenly divisible by three before it.&lt;/p&gt;

&lt;p&gt;Is there a good way to do this?&lt;/p&gt;

Thanks </field>
<field name="reputation">
6</field>
</data>
</node>
