Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching words with ANY repeating characters

Tags:

regex

perl

Looking for a perl one-liner what will find all words with the next pattern:

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

For one character, it is easy, e.g. for 'a'

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

but I want search for ANY character, so, looking for one regex for finding all words like:

azalea   #repeating a
baobab   #repeating b
cyclic   #c

and so on..

tried this:

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

but not works.

like image 341
kobame Avatar asked Jun 14 '12 23:06

kobame


People also ask

How do you find a repeating character?

To find the duplicate character from the string, we count the occurrence of each character in the string. If count is greater than 1, it implies that a character has a duplicate entry in the string. In above example, the characters highlighted in green are duplicate characters.

How do you find the first repeated and non repeated character in a string?

Step 1 : Define one HashMap called charCountMap with Character as key and Integer as value. This map will hold the characters and their count in the given string. Step 2 : Convert inputString to char array called strArray. Step 3 : Iterate through all chars of strArray and update their occurrences in charCountMap.

How can you find the first non repeated character in a word?

Using the indexOf() and lastIndexOf() method, we can find the first non-repeating character in a string in Java. The method indexOf() returns the position of the first occurrence of a given character in a string whereas method lastIndexOf() returns the position of the last occurrence of a given character in a string.


2 Answers

(?:(?!STRING).)

is to

(?:STRING)

as

[^CHAR]

is to

CHAR

so you could use

/
   ^
   (\pL)
   (?:
      (?:(?!\1).)+
      \1
   ){2}
   \z
/sx
like image 77
ikegami Avatar answered Nov 02 '22 18:11

ikegami


This is the best regex I could come up with:

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

Tested on RegexPal.

like image 25
Kendall Frey Avatar answered Nov 02 '22 17:11

Kendall Frey