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

Re: Using regex to match double letters, and only double letters

by Locutus (Beadle)
on Apr 16, 2018 at 15:01 UTC ( #1212994=note: print w/replies, xml ) Need Help??


in reply to Using regex to match double letters, and only double letters

Duck Duck Go pointed me to this quite old thread when searching for "regular expression matching double letters". Although the OP emphasized "and only double letters" in its title all solutions posted so far also match double letters which are part of triple, quadruple, ... letters (e.g. in "Helllo", "cooool") and within 12 years no one seems to have cared about that. For what it's worth I do care about it and found it surprisingly difficult to come up with a regex that matches doubles only. One of my closest approaches is

/(.)((?!\1).)\2(?!\2)/

which uses a negative lookahead to make sure there's something else before the double letter and another negative lookahead to make sure there's something else following the double letter. However, this regex still matches the "something else before" character, too - as I have to use a capturing group for it in order to have a corresponding backreference to use in the lookahead assertion.

Could it be that finding doubles only is not a regular problem in the sense of Theoretical Computer Science? Or is it just me being unable to find a proper solution?

Replies are listed 'Best First'.
Re^2: Using regex to match double letters, and only double letters
by AnomalousMonk (Bishop) on Apr 18, 2018 at 01:36 UTC

    Further to AnonyMonk's Re^2: Using regex to match double letters, and only double letters regex:   Note that the look-ahead is not strictly necessary:
        / (.) \g-1 (?: \g-1+ (*SKIP)(*FAIL))? /x
    works | seems to work equally well. Since Perl version 5.10+ regex extensions must be used (for (*SKIP) (*FAIL)) anyway, I've also used \gn relative backreferencing so the regex can be defined in a  qr// more safely; this can be more convenient in an extractive application.


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

Re^2: Using regex to match double letters, and only double letters
by choroba (Archbishop) on Apr 16, 2018 at 15:39 UTC
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; for my $s (qw( xxabyycdzz axxbmmmcyyd xxyy exxyy xxyyf xxgyy mmm mmmen +nn )) { say for '---', $s; say $2 while $s =~ /(?|^((.))\2(?!\2) |(?<=(.)(?!\1))(.)\2(?!\2))/gx; }

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Your regex (as a whole) matches the "something else before" character as well - printing the content of a certain capture group is cheating! ;-)

Re^2: Using regex to match double letters, and only double letters
by Anonymous Monk on Apr 16, 2018 at 18:01 UTC

    Perl regex engine has support for (*SKIP) since version 5.010; so you could write:

    /(.)\1((?!\1)|\1*(*SKIP)(*FAIL))/

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2020-10-25 06:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (249 votes). Check out past polls.

    Notices?