Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding duplicate words within a string regex C/W

I'm currently dabbing in regex in Java, and want to try and find duplicate words in strings. If I inputted a string such as 'This this is great.'. I was using \\b(\\w+) \\1\\b, but that only recognizes two duplicate words, such as 'this this' in a string.

Any help regarding this?

like image 996
Jake Roper Avatar asked Oct 21 '22 13:10

Jake Roper


1 Answers

Add the "ignore case" switch (?i) to your regex:

(?i)\\b(\\w+) \\1\\b

Alternatively, you could fold the input to lower case first:

input.toLowerCase()

Note: If you're using String.matches(), the regex must match the entire input, so you'd add .* to both ends of your regex:

.*(?i)\\b(\\w+) \\1\\b.*
like image 188
Bohemian Avatar answered Oct 27 '22 09:10

Bohemian