Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

First of all, I don't understand if you want all three-letter immediately repeated words, as your OPed regex
implies, or all three-letter words that are repeated anywhere else in the string, as your code implies.

In the spirit of the second interpretation (and counting them) (requires Perl version 5.10+ for  \g-1 construct):

c:\@Work\Perl>perl -wMstrict -MData::Dump -le "use 5.010; ;; my $term = 'Dit is het eerste het is xhetx xhet hetx niet het laatste + Dit'; ;; my $word = qr{ \b \w{3} \b }xms; ;; my %repeats; while ($term =~ m{ ($word) (?= .*? (?= $word) \g-1) }xmsg) { $repeats{$1}++; } ;; dd \%repeats; " { Dit => 1, het => 2 }
Change the definition of  $word to whatever best suits your requirements. | See Update 3 below.


  1. Added info about 5.10+ requirement.
  2. BTW, the "regex"  /\b(\w\w\w)\s\g1\b/;/ doesn't actually compile. It looks like it might be a piece of something else, e.g., a substitution:
    (update: or maybe the / at the end is completely extraneous and the statement  /\b(\w\w\w)\s\g1\b/; was intended — that would work)
  3. When I wrote "Change the definition of  $word to whatever best suits your requirements" above, what I had in mind was that any  $word definition used in the context of the
        m{ ($word) (?= .*? (?= $word) \g-1) }xmsg
    match would be assured to match repeated words per my understanding of the OP. Not so, and it's easy to manufacture a counterexample. Of course, it's also easy to fix the counterexample to avoid the problem, but the fix requires knowledge of internal details of the  $word definition, and this is exactly what I was trying to avoid. In a further iteration, I can come up with a match regex that seems to fulfill all my (admittedly rather arbitrary) requirements, but it's not well tested and I don't really love it as I should. So as always, Caveat Programmor.

Give a man a fish:  <%-{-{-{-<

In reply to Re: regex issue by AnomalousMonk
in thread regex issue by Anonymous Monk

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (4)
    As of 2020-01-23 22:52 GMT
    Find Nodes?
      Voting Booth?