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?
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.*
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With