Find Duplicate Words in C / W Regular Expression

I am currently using regex in Java and want to try and find duplicate words in strings. If I entered a line such as "It's great." I used \\b(\\w+) \\1\\b, but it only recognizes two duplicate words, such as "this this" in a string.

Any help on this?

+5
source share
2 answers

Add the ignore case switch to your regular expression (?i):

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

Alternatively, you can first collapse the input to lowercase:

input.toLowerCase()

. String.matches(), , .* :

.*(?i)\\b(\\w+) \\1\\b.*
+2
String pattern = "\\b(\\w+)(\\b\\W+\\b\\1\\b)*"; 
Pattern r = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);

Matcher.group() Matcher.group(1), .

+1

All Articles