Reputation: 21
I am trying to match words that have the same letter repeated as the second letter, the ending character and an in the middle 1 times, and I need to capture the second letter and match the whole line.
words examples
syzygy
error
banana
I tried doing
^[a-z]([a-z])[a-z]+[a-z]+\1$
and this matches the lines and captures my second letter, but I need to make sure the second letter is repeated
s(y)z y <-same as second character g y <- ends in same character
so I need to be sure in a string that y is at the second position, 1 time in the middle, and ending of the string
Upvotes: 2
Views: 1008
Reputation: 70722
One way you could do this, if I understand you correctly is using a negative look ahead.
^.(.)(?:(?!\1).)*\1(?:(?!\1).)*\1$
The dot .
matches any single character, except line break characters. By using \1
, we reference the match that was saved in the first capture group.
See regular expression explanation
See live demo of capturing the second character and matching the whole string.
See live demo of how \1
is matching repeated characters.
Upvotes: 1
Reputation:
# ^[a-z]([a-z])[a-z]*\1[a-z]*\1$
^
[a-z]
( [a-z] ) # (1), Second letter
[a-z]*
\1 # A Backref to second letter in the middle
[a-z]*
\1 # A Backref to second letter at the end
$
Upvotes: 1