]
> Sent: Tuesday, June 12, 2007 3:20 PM
> To: Perl beginners
> Subject: Re: regex for matching repeated strings
>
> yitzle wrote:
> > Issues with both methods:
> >
> > John's doesn't work for this data:
> > aa
> > aa
> > bbb
>
On Jun 13, 5:21 am, [EMAIL PROTECTED] (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".
On 6/13/07, James <[EMAIL PROTECTED]> wrote:
Thanks all, I have something working
> $data =~ s/(.*\n)(?=\1)//g;
Can anyone explain the (?=\1) bit? I get the search replace.
J.
Didn't understand it myself, but see:
http://www.boost.org/libs/regex/doc/syntax_perl.html
Search for Back reference
Thanks all, I have something working
> $data =~ s/(.*\n)(?=\1)//g;
Can anyone explain the (?=\1) bit? I get the search replace.
J.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/
yitzle wrote:
Issues with both methods:
John's doesn't work for this data:
aa
aa
bbb
cc
cc
I would expect:
aa
bbb
cc
I would get:
aa
bbb
cc
cc
It works for me:
$ perl -le'
my $data = q[aa
aa
bbb
cc
cc
];
print $data;
$data =~ s/(.*\n)(?=\1
On 6/12/07, yitzle <[EMAIL PROTECTED]> wrote:
Issues with both methods:
snip
If you only want to reduce runs (as opposed to removing dups) then
#!/usr/bin/perl
use strict;
use warnings;
my $cur = undef;
while () {
print unless defined $cur and $_ eq $cur;
$cur = $_;
}
__DATA__
Issues with both methods:
John's doesn't work for this data:
aa
aa
bbb
cc
cc
I would expect:
aa
bbb
cc
I would get:
aa
bbb
cc
cc
With the solution by Chas and the data:
aa
aa
bbb
aa
aa
I expect:
aa
bbb
aa
I get:
aa
bbb
--
To un
On 6/12/07, James <[EMAIL PROTECTED]> wrote:
Is there a way of writing a regex to find 1 or more occurances of specific
text string, and replace with a single occurance.
Possibly, but using a hash is a lot easier and probably more efficient:
#!/usr/bin/perl
use strict;
use warnings;
my %h;
James wrote:
Is there a way of writing a regex to find 1 or more occurances of specific
text string, and replace with a single occurance.
e.g.:
AI01
AI01
AI01
AI01
needs to be replaced with
AI001
thus (m/(AI\d{6}\n)/) will find one occurance and capture as $1 (assumin