|Perl Monk, Perl Meditation|
choroba's substr 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).
Here is a simple regex solution:
Note that pos($string) returns the position where the next match 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.
Update: as I think about it more, using pos() 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:
@- is a special variable containing the offset of each subpattern in the previous match. $- will always refer to the beginning of the match (I have added one to give you the one-based position).
When's the last time you used duct tape on a duct? --Larry Wall
In reply to Re: Is it possible to get all tetra words with correct starting position using a better code within a loop?