U
usenet
One particular aspect of a question in another newsgroup
(http://tinyurl.com/cbakx) interested me; I played around with some
solutions but couldn't come up with one that I thought was elegant. So
I thought I would introduce the question to this group for further
enlightenment.
<paraphrase> of the OP's question:
Suppose I have a string of characters: "abCCCdefg". I want to match
three consecutive occurrences of any character in a class. In this
example, my expression would match 'CCC'. OK, that's easy:
#!/usr/bin/perl
use warnings; use strict;
my $string = "abCCCdefg";
print "Match!\n" if $string =~ /([\w\d_-]+)\1{2}/;
__END__
But, suppose I wanted to constrain the match so that it would match
three consecutive occurrences, but NO MORE than three. In other words,
'abCCCCCdefg' would NOT match. </paraphrase>
I thought I could propose an 'elegant' answer like this:
print "Match!\n" if $string =~ /([\w\d_-]+)\1{2}[^\1]/;
but that doesn't work (it seems that \1 gets "used up" somehow). Of
course, I could write a bunch of code to do it... that's trivial to do
(but ugly, IMHO).
Are there any elegant ideas?
(http://tinyurl.com/cbakx) interested me; I played around with some
solutions but couldn't come up with one that I thought was elegant. So
I thought I would introduce the question to this group for further
enlightenment.
<paraphrase> of the OP's question:
Suppose I have a string of characters: "abCCCdefg". I want to match
three consecutive occurrences of any character in a class. In this
example, my expression would match 'CCC'. OK, that's easy:
#!/usr/bin/perl
use warnings; use strict;
my $string = "abCCCdefg";
print "Match!\n" if $string =~ /([\w\d_-]+)\1{2}/;
__END__
But, suppose I wanted to constrain the match so that it would match
three consecutive occurrences, but NO MORE than three. In other words,
'abCCCCCdefg' would NOT match. </paraphrase>
I thought I could propose an 'elegant' answer like this:
print "Match!\n" if $string =~ /([\w\d_-]+)\1{2}[^\1]/;
but that doesn't work (it seems that \1 gets "used up" somehow). Of
course, I could write a bunch of code to do it... that's trivial to do
(but ugly, IMHO).
Are there any elegant ideas?