<?xml version="1.0" encoding="windows-1252"?>
<node id="1005084" title="Re: Is it possible to get all tetra words with correct starting position using a better code within a loop?" created="2012-11-22 04:20:18" updated="2012-11-22 04:20:18">
<type id="11">
note</type>
<author id="27571">
ColonelPanic</author>
<data>
<field name="doctext">
&lt;p&gt;choroba's &lt;code&gt;substr&lt;/code&gt; solution is the best for the problem as you have presented it.  However, a regex solution could be useful if you will need to introduce other requirements (such as only matching certain characters).&lt;/p&gt;
&lt;p&gt;Here is a simple regex solution:&lt;/p&gt;
&lt;code&gt;
use strict;
use warnings;

my $string = 'ABCDEFGHIJKL';
print "$1$2 at ".pos($string)."\n" while ($string =~ /(.)(?=(...))/g);
&lt;/code&gt;
&lt;p&gt;Note that &lt;code&gt;pos($string)&lt;/code&gt; returns the position where the &lt;i&gt;next match&lt;/i&gt; on $string will start.  In this case, that happens to be exactly what you want: it is one greater than the (zero-based) position of the current match, meaning it is the position of the current match with one-based indexing.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; as I think about it more, using &lt;code&gt;pos()&lt;/code&gt; is probably not the best.  It is misleading to use it to refer to the match start position, because that is not what it really means.  It works in this case, but the code would break if you changed your regex to match something different.  Here is the correct way to get the position of the beginning of your match:&lt;/p&gt;
&lt;code&gt;
print "$1$2 at ". ($-[0] + 1) ."\n" while ($string =~ /(.)(?=(...))/g);
&lt;/code&gt;
&lt;p&gt;&lt;code&gt;@-&lt;/code&gt; is a special variable containing the offset of each subpattern in the previous match. &lt;code&gt;$-[0]&lt;/code&gt; will always refer to the beginning of the match (I have added one to give you the one-based position).&lt;/p&gt;


&lt;div class="pmsig"&gt;&lt;div class="pmsig-27571"&gt;
&lt;br&gt;&lt;br&gt;When's the last time you used duct tape on a duct?  --Larry Wall
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1005077</field>
<field name="parent_node">
1005077</field>
</data>
</node>
