Alright, here is a revised version of johngg's solution, that prevents the redundant double matching, while at the same time keeping the matching logic self-contained within the regex.
Instead of moving pos() forward manually (like I suggested in the discussion thread for johngg's solution), it lets the regex engine do this implicitly by having it gobble up $n characters (if available) after matching the zero-width look-ahead that contains the capture group:
# 0 5 10 15 20 25 30 35
# ' ' ' ' ' ' ' '
$_ = q{.....fred1..fred2...fred3....fred4..};
# ----++++----||||----||||----++++---- $n = 4
# -----||||+-----+++++||||-+++++-----+ $n = 5
my $capture = qr([0-9]\.+); # the (....) in the OP's specification
for my $n ( 4, 5 )
{
say "\$n = $n";
while ( m[\G(?:.{$n})*?(?=fred($capture)).{0,$n}]g ) {
say " matched 'fred$1' at pos @{[pos($_)-$n]} (gobbled '$&')";
}
}
Output:
$n = 4
matched 'fred2...' at pos 12 (gobbled '.....fred1..fred')
matched 'fred3....' at pos 20 (gobbled '2...fred')
$n = 5
matched 'fred1..' at pos 5 (gobbled '.....fred1')
matched 'fred3....' at pos 20 (gobbled '..fred2...fred3')
Note that if length("fred$1") > $n, it will actually start looking for the next "fred" while still whithin the part matched by $1. If this must be avoided, I guess manual pos()-incrementing is still the best bet.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|