Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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]

Comment on Re: Pattern Finding
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://111624]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2015-11-29 10:02 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (750 votes), past polls