You don't need to capture everything if all you want to do is replace one field.

Assuming all lines that begin with 'sip', have the appropriate number of fields, what your regex above translates to is:Replace the second last set of digits with a running count.

If you can isolate and match just the bit you want to replace, you don't have to capture everything else. By using zero length assertions to bracket the bit you want to replace, you don't need explicit captures and the replacement only consists of the count:

use strict; my $count = 1; while (<>) { if ( m/^\^sip/ ) { s[ ## No need to explicitly capture anything (?<=\s) ## 2nd last num field cannot be 4 digits \d{1,3} ## only this is replaced. (?= \s+ \d+ $ ) ## Ensure there's one more before the EOS ][$count]x; $count++; } print ; }

If you really need to verify that modified lines contain exactly 17 space delimited fields preceding the replacement, then just capture the whole thing as a single entity:

use strict; my $count = 1; while (<>) { if ( m/^\^sip/ ) { s[ ( (?: \S+ \s+ ){17} ) ## Capture the 17/34 fields to $1 \d{1,3} ## No need to capture (?= \s+ \d+ $ ) ## Ensure there's one more before the EOS ][$1$count]x; $count++; } print ; }

Also, are you sure you want to increment the count even if the replacement doesn't happen?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^4: seeking improvement in my smiple program using regular expression by BrowserUk
in thread seeking improvement in my smiple program using regular expression by convenientstore

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.