-1

How to define regular expression in java which accepts only those words which starts and end with a

Mayur
  • 1
  • 1

1 Answers1

0

Something like this should work for you \b(\w)\w+\1\b for starts and ends with same letter (your question title).

Explanation:

\b signifies a word boundary - basically anything \w would not match.

(\w) match a single word character and captures it to group 1.

\w+ matches for one or more word characters (to match the rest of the word)

\1 is a back reference to what the capture group 1 matched, to ensure last letter is same as first

CrayonViolent
  • 32,111
  • 5
  • 56
  • 79