Search for words with ANY duplicate characters

We are looking for one perl slot that will find all words with the following pattern:

X(not_X_chrs)X(not_X_chrs)X    e.g. cyclic

For a single character, this is easy, for example. for 'a'

perl -nle 'print if /^a[^a]+a[^a]+a$/' < /usr/share/dict/web2

but I want to search for the character ANY , therefore, looking for one regular expression to find all such words as:

azalea   #repeating a
baobab   #repeating b
cyclic   #c

etc.

tried this:

perl -nle 'print if m/^([a-z])[^$1]+$1[^$1]+$1$/i' </usr/share/dict/web2

but does not work.

+5
source share
4 answers
(?:(?!STRING).)

has the meaning

(?:STRING)

and

[^CHAR]

has the meaning

CHAR

so you can use

/
   ^
   (\pL)
   (?:
      (?:(?!\1).)+
      \1
   ){2}
   \z
/sx
+6
source

This is the best regex I could come up with:

^([a-z])((?:(?!\1).)+\1){2}$

Tested on RegexPal .

+3
source

:

^(\w)(?>\w*?\1){2}$

Altho, , 0 .

1 :

^(\w)(?>(?!\1)\w+?\1){2}$
0

perlretut , ( ) \g1. 5.14. 5.12.2, \1.

:

use strict; use warnings;
use 5.12.2;
use feature qw(say);
for (qw/ azalea baobab cyclic deadend teeeeeestest doesnotwork /) {
  say if m/^([a-z])[^\1]+\1[^\1]+\1$/i;
}

YAPE:: Regex::

use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new(qr/^([a-z])[^\1]+\1[^\1]+\1$/i)->explain();

:

The regular expression:

(?i-msx:^([a-z])[^\1]+\1[^\1]+\1$)

matches as follows:


use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new(qr/^([a-z])[^\1]+\1[^\1]+\1$/i)->explain();

NODE                     EXPLANATION
----------------------------------------------------------------------
(?i-msx:                 group, but do not capture (case-insensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [a-z]                    any character of: 'a' to 'z'
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  [^\1]+                   any character except: '\1' (1 or more
                           times (matching the most amount possible))
----------------------------------------------------------------------
  \1                       what was matched by capture \1
----------------------------------------------------------------------
  [^\1]+                   any character except: '\1' (1 or more
                           times (matching the most amount possible))
----------------------------------------------------------------------
  \1                       what was matched by capture \1
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

. , perl -e 'print if m/^([a-z])[^\1]+\1[^\1]+\1$/i'.

, perl -w -e 'print if m/(as)$1/', :

$ perl -w -e 'print if m/(a)$1/' asdf
Use of uninitialized value $1 in regexp compilation at -e line 1.
Use of uninitialized value $_ in pattern match (m//) at -e line 1.

, ololololo.

0

All Articles