I need to have a Regular Expression for the below Input and Output
Sample Input:
TEST_ABC
TEST_DEF
TEST_XYZ
Sample Output:
TEST1_ABC
TEST2_DEF
TEST3_XYZ
No Restrictions on sequencing the input parameters.
Replace
(?=_)
with
1
(?=_)
"matches" the position before the _
using a positive look-ahead. It doesn't match any actual characters.
Replacing with 1
, inserts it at the matched position.
Regular expressions do not support incrementing numbers, because regular expressions are about patterns not numbers.
However, perl
can do something like this:
#!/usr/bin/env perl
use strict;
use warnings;
my $count;
while (<DATA>) {
s/(?=_)/++$count/e;
print;
}
__DATA__
TEST_ABC
TEST_DEF
TEST_XYZ
Which inserts an incrementing count before the first underscore in each line.
As a one liner, this'd be;
perl -pe 's/(?=_)/++$c/e'