Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for a double word

Tags:

regex

repeat

I am trying to write a regular expression that will detect a word that is repeated twice, e.g., greatgreat or appleapple. I could not find any way to do this, after hours of trying I am posting it here to see if anyone can help.

like image 922
Meir Avatar asked Aug 06 '26 12:08

Meir


1 Answers

Try this (example on RegExr):

(\w+)\1

If you want to make sure that the whole sting is a doubled word add anchors:

^(\w+)\1$

For more information see the docs - "Round brackets for grouping and backreferences".

like image 120
KL-7 Avatar answered Aug 08 '26 06:08

KL-7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!