Match repeating pattern

Match repeating pattern

am 08.09.2007 08:41:33 von serman_d

$ perl -wle 'print 1/7'
0.142857142857143

How do I match the a repeating pattern within a string? Of the above
string I want to match '142857'.

Thanks.

Serman D.
--

Re: Match repeating pattern

am 08.09.2007 08:51:41 von usenet

On Sep 7, 11:41 pm, "Serman D." wrote:
> How do I match the a repeating pattern within a string? Of the above
> string I want to match '142857'.

Please clarify: What if the string is 0.2323232323232323

Would you want to match 23? or 2323? or 232323? Or 232232323?

All of these patterns are repeated in the string.

If the string is: 0.111A111B111

Would you expect to match 111? (ie, matches not sequential)

--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

Re: Match repeating pattern

am 08.09.2007 09:07:57 von serman_d

On Sep 8, 8:51 am, use...@DavidFilmer.com wrote:

> Please clarify: What if the string is 0.2323232323232323

I want to match the longest, sequential repeating pattern within the
string.

--
Serman D.

Re: Match repeating pattern

am 08.09.2007 11:23:25 von Big and Blue

Serman D. wrote:
>
>> Please clarify: What if the string is 0.2323232323232323
>
> I want to match the longest, sequential repeating pattern within the
> string.

So, for that string you'd want 23232323?

Try this:

#!/usr/bin/perl
#

while (<>) {
if (/(.+)\1/) {
print "Found $1\n";
}
else {
print "No Repeat\n";
}
}

If you actually want just 23 above then change the .+ to .+?.

This isn't perfect, as it will actually find the first repeat, rather than
the longest, but it might help you.


--
Just because I've written it doesn't mean that
either you or I have to believe it.

Re: Match repeating pattern

am 08.09.2007 12:22:16 von Tad McClellan

Serman D. wrote:
> $ perl -wle 'print 1/7'
> 0.142857142857143
>
> How do I match the a repeating pattern within a string? Of the above
> string I want to match '142857'.


perl -le 'print $1 if (1/7) =~ /\.(\d+)\1+/'


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Match repeating pattern

am 08.09.2007 12:22:17 von Tad McClellan

Serman D. wrote:
> On Sep 8, 8:51 am, use...@DavidFilmer.com wrote:
>
>> Please clarify: What if the string is 0.2323232323232323
>
> I want to match the longest, sequential repeating pattern within the
> string.


What if the string is 12341234.567567 ?

perl -le 'print $1 if "12341234.567567" =~ /(\d+)\1+/'


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"