Hello.

I'm trying to expand my knowledge of regexen, especially more advanced uses for them, beyond simple things like matching e-mail address, for example ;)

I'm trying to find a repeated substring in a larger string. Given the following code:

#!/usr/bin/perl use strict; use warnings; my @found; # 0 1 2 3 4 5 # 012345678901234567890123456789012345678901234567890 $_ = q'abczdefzabcghijklzabczaerabrtyuabcethdauthabkudiabc'; # abc abc abc abc abc # 0 8 18 31 48 m< ( (?{ push @found, pos() }) abc # the string I know which repeats ) \G .+? \1 >gx; print join ', ', @found, "\n";

Which prints: 0, 8, 18, 31, 48, - exactly the positions I know the substring is in relative to the larger string.

But suppose I don't know ahead of time _what_ the repeating pattern is, but would still like to detect it? Leaving the rest of the code alone, I change the regex to:

m< ( (?{ push @found, pos() }) [a-z]{3} # suppose i'm looking for a series of 3 repeating char +acters... ) \G .+? \1 >gx;

This doesn't work at all as i'd hope; it prints 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,

Can anyone help me out with at the very least a shove in the right direction? I've tried as many variations as I can think of, inserting variations of (?{ print pos(), $^N }) into various places, and just can't figure out what I need to change to achieve my desired results.



--chargrill
$,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}

In reply to Finding a repeating substring with a regex by chargrill

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.