in reply to Re: Remove all lines, except those starting with pattern
in thread Remove all lines, except those starting with pattern
Note that in the comments of the code examples in the Positive and Negative Look-behind sections, e.g.
'(?<=this\ )that' # match 'that' when followed by 'this '
'(?<!this\ )that' # match 'that' when NOT followed by 'this '
the word "followed" should be "preceded" in both cases, i.e.
'(?<=this\ )that' # match 'that' when preceded by 'this '
'(?<!this\ )that' # match 'that' when NOT preceded by 'this '
respectively.
Both the PRECEDING_PATTERN and the FOLLOWING_PATTERN are matched as the 'look-around' patterns, but neither are captured to a variable, only what is matched as part of your (PATTERN_OF_INTEREST) capture group ...
As an interesting (I hope) side note, a capture group embedded within a positive lookaround assertion will capture something:
c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'foobar'; ;; print qq{positive look-behind capture: '$1'} if $s =~ m{ (?<= (foo)) + bar }xms; print qq{positive look-ahead capture: '$1'} if $s =~ m{ foo (? += (bar)) }xms; " positive look-behind capture: 'foo' positive look-ahead capture: 'bar'
An embedded positive look-ahead capture is a way to capture and extract certain overlapping matches:
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $s = 'abcd efgh'; ;; my @caps = $s =~ m{ \w+ }xmsg; dd 'non-overlapping captures: ', \@caps; ;; @caps = $s =~ m{ (?= (\w+)) }xmsg; dd 'overlapping captures: ', \@caps; " ("non-overlapping captures: ", ["abcd", "efgh"]) ( "overlapping captures: ", ["abcd", "bcd", "cd", "d", "efgh", "fgh", "gh", "h"], )
Update: Slight, essentially trivial wording and emphasis changes.
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Remove all lines, except those starting with pattern
by perlygapes (Beadle) on Aug 14, 2018 at 14:44 UTC |