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.
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.
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.
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.
(?:(?!STRING).)
is to
(?:STRING)
as
[^CHAR]
is to
CHAR
so you could use
/
^
(\pL)
(?:
(?:(?!\1).)+
\1
){2}
\z
/sx
This is the best regex I could come up with:
^([a-z])((?:(?!\1).)+\1){2}$
Tested on RegexPal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With