Skip to main content
added 109 characters in body
Source Link
Mustahsan
  • 3.8k
  • 1
  • 22
  • 35

you can use regex to do this for you. sample code:

String regex = "\\b(\\w+)\\b\\s*(?=.*\\b\\1\\b)"; input = input.replaceAll(regex,""); 
  1. \b Matches a word boundary position between a word character and non-word character or position (start / end of string).
  2. \w Matches any word character (alphanumeric & underscore).
  3. \b Matches a word boundary position between a word character and non-word character or position (start / end of string).
  4. \s Matches any whitespace character (spaces, tabs, line breaks).
  5. * Match 0 or more of the preceding token.
  6. (?= Matches a group after the main expression without including it in the result.
  7. . Matches any character except line breaks.
  8. \1 Matches the results of capture group #1 in step 2.

Note: It is important to use word boundaries here to avoid matching partial words.

Here's a link to regex demo and explaination : RegexDemo

you can use regex to do this for you. sample code:

String regex = "\\b(\\w+)\\b\\s*(?=.*\\b\\1\\b)"; input = input.replaceAll(regex,""); 

Here's a link to regex demo and explaination : RegexDemo

you can use regex to do this for you. sample code:

String regex = "\\b(\\w+)\\b\\s*(?=.*\\b\\1\\b)"; input = input.replaceAll(regex,""); 
  1. \b Matches a word boundary position between a word character and non-word character or position (start / end of string).
  2. \w Matches any word character (alphanumeric & underscore).
  3. \b Matches a word boundary position between a word character and non-word character or position (start / end of string).
  4. \s Matches any whitespace character (spaces, tabs, line breaks).
  5. * Match 0 or more of the preceding token.
  6. (?= Matches a group after the main expression without including it in the result.
  7. . Matches any character except line breaks.
  8. \1 Matches the results of capture group #1 in step 2.

Note: It is important to use word boundaries here to avoid matching partial words.

Here's a link to regex demo and explaination : RegexDemo

added 109 characters in body
Source Link
Mustahsan
  • 3.8k
  • 1
  • 22
  • 35

you can use regex to do this for you. sample code:

String regex = "\\b(\\w+)\\b\\s*(?=.*\\b\\1\\b)"; input = input.replaceAll(regex,""); 

Here's a link to regex demo and explaination : RegexDemo

you can use regex to do this for you. sample code:

String regex = "\\b(\\w+)\\b\\s*(?=.*\\b\\1\\b)"; input = input.replaceAll(regex,""); 

you can use regex to do this for you. sample code:

String regex = "\\b(\\w+)\\b\\s*(?=.*\\b\\1\\b)"; input = input.replaceAll(regex,""); 

Here's a link to regex demo and explaination : RegexDemo

Source Link
Mustahsan
  • 3.8k
  • 1
  • 22
  • 35

you can use regex to do this for you. sample code:

String regex = "\\b(\\w+)\\b\\s*(?=.*\\b\\1\\b)"; input = input.replaceAll(regex,"");