in reply to Removing matched pattern except the first pattern
When I first read the OP, the phrase "matched pattern" triggered an "I know, I'll use a regex..." knee-jerk response, and I formulated the problem statement "keep the first n occurrences of a pattern in a string along with any intervening matter and delete all occurrences thereafter".
It turns out that a regex-based approach is not appropriate for the OPed problem unless it be a "keep all lines that match a given regex" strategy, which would, IMHO, be quite good because it combines a possibly quite large element of validation with data extraction and is also completely scaleable.
In any event, proceeding along the lines of my first-but-not-necessarily-best thought, I came up with this, which may be of interest:
>perl -wMstrict -le "my $s = 'x foo1 foo2 x foo3 x yfoo9 foo4 foo5 foo9y x foo6 foo7 x'; print qq{'$s' \n}; ;; my $pat = qr{ \b foo \d \b }xms; ;; for my $n (0 .. 4) { (my $t = $s) =~ s{ \A (?: .*? $pat){$n} \K | $pat }''xmsg; print qq{'$t'}; } " 'x foo1 foo2 x foo3 x yfoo9 foo4 foo5 foo9y x foo6 foo7 x' 'x x x yfoo9 foo9y x x' 'x foo1 x x yfoo9 foo9y x x' 'x foo1 foo2 x x yfoo9 foo9y x x' 'x foo1 foo2 x foo3 x yfoo9 foo9y x x' 'x foo1 foo2 x foo3 x yfoo9 foo4 foo9y x x'
Update: I should add that the \K regex operator used above is available with Perl versions 5.10+.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Removing matched pattern except the first pattern
by rahulruns (Scribe) on May 29, 2013 at 05:28 UTC |