On Jun 13, 5:21 am, jlum...@arrowt.co.uk (James) wrote:
Thanks all, I have something working
$data =~ s/(.*\n)(?=\1)//g;
Can anyone explain the (?=\1) bit? I get the search replace.
Which part do you not understand? The (?=) or the \1 or both?
(?= ) is a "positive lookahead assertion". It "peeks" into the
pattern match to determine if the next thing matches its contents, but
it doesn not actually match those contents. It doesn't move the
internal position pointer along, and whatever is in the (?= ) is not
part of the actual match so will not be replaced.
\1 within a pattern match means exactly what $1 will mean when the
pattern match is finished. That is, it's whatever was matched by the
first capturing parentheses in this pattern match. In this case,
So this pattern is searching for 0 or more of any character, followed
by the newline, and then checks to see if the next thing after that is
exactly what was matched again. If so, the entire *MATCH* is replaced
with nothing. Since the second instance was of the .*\n was not
actually matched, just looked for, it does not get replaced.
For more information,