Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Pattern Finding

by runrig (Abbot)
on Sep 11, 2001 at 03:08 UTC ( #111624=note: print w/replies, xml ) Need Help??

in reply to Pattern Finding

Untested, and not very thought out:
$_ = "hellohiworldhellohiworldhellohiworldhellohiworld"; if (/^((.+?)\2+)$/) { print "[$1][$2]\n"; }
Update: Hmm, this does not work, and I'm not sure why (regex bug? or bad code?) :-(

Ok, got the above code to work at least the way I was expecting, but still probably not what you want.

<update>Though here's something that seems to do what you want:

use warnings; use strict; $_ = "helloworldhellohellohihellohiworld"; my %pttrns; PTTRN: { if (/\G(.{2,})(?=.*?\1)/g) { $pttrns{$1}++; SKIP: { my $again; for my $pat (keys %pttrns) { if (substr($_, pos, length $pat) eq $pat) { pos() += length($pat); redo SKIP; } } } redo PTTRN; } } print "[$_]\n" for keys %pttrns; ~/tmp >./tst [world] [hello] [hi]

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://111624]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2017-08-19 22:55 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (312 votes). Check out past polls.