regex - Match for words that have same letters -


i trying match words have same letter repeated second letter, ending character , in middle 1 times, , need capture second letter , match whole line.

words examples

syzygy error banana 

i tried doing

^[a-z]([a-z])[a-z]+[a-z]+\1$ 

and matches lines , captures second letter, need make sure second letter repeated

 s(y)z y <-same second character g y <- ends in same character 

so need sure in string y @ second position, 1 time in middle, , ending of string

one way this, if understand correctly using negative ahead.

^.(.)(?:(?!\1).)*\1(?:(?!\1).)*\1$ 

the dot . matches single character, except line break characters. using \1, reference match saved in first capture group.

see regular expression explanation

see live demo of capturing second character , matching whole string.

see live demo of how \1 matching repeated characters.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -