in reply to Re^3: seeking improvement in my smiple program using regular expression
in thread seeking improvement in my smiple program using regular expression

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."