Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Pattern Finding

by runrig (Abbot)
on Sep 11, 2001 at 03:08 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-29 10:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found