Delete individual commas, but do not delete 3 adjacent commas in a sentence

In the sentence below:

String res = [what, ask, about, group, differences, , , or, differences, in, conditions, |? |] 

I want to remove one comma (,), but I do not want to remove three adjacent commas.

I tried with this regex: res.replaceAll("(,\\s)^[(,\\s){3}]", " ") , but it doesn't work.

+4
source share
4 answers

An easy way to do this is by chaining two replaceAll bindings, instead of using just one template:

 String input = "[what, ask, about, group, differences, , , or, differences, in, conditions, |? |]"; System.out.println( input // replaces // | comma+space not preceded/followed by other comma // | | with space .replaceAll("(?<!, ), (?!,)", " ") // replaces // | 3 consecutive comma+spaces // | | with single comma+space .replaceAll("(, ){3}", ", ") ); 

Exit

 [what ask about group differences, or differences in conditions |? |] 
+3
source

You can use this code with a replacement in the find method:

 String s = "[what, ask, about, group, differences, , , or, differences, in, conditions, |? |]"; StringBuffer result = new StringBuffer(); Matcher m = Pattern.compile("((?:\\s*,){3})|,").matcher(s); while (m.find()) { if (m.group(1) != null) { m.appendReplacement(result, ","); } else { m.appendReplacement(result, ""); } } m.appendTail(result); System.out.println(result.toString()); 

Watch the IDEONE demo

Exit: [what ask about group differences, or differences in conditions |? |] [what ask about group differences, or differences in conditions |? |]

The regular expression - ((?:\\s*,){3})|, - corresponds to 2 alternatives: either 3 commas separated by an optional space (which is fixed), or just a comma. If we get a hold, replace it with a comma. If the capture is zero, we match one comma, delete it.

+2
source

You can also use:

 String res = "[what, ask, about, group, differences, , , or, differences, in, conditions, |? |]"; res.replaceAll("(?<=\\w),(?!\\s,)|(?<!\\w),\\s",""); 
  • (?<=\\w),(?!\\s,) are commas preceding the word, and do not fall under another comma,
  • (?<!\\w),\\s - the comma does not precede the word
+1
source

Another possible approach:

 .replaceAll("(,\\s){2,}|,", "$1") 
  • (,\\s){2,} will try to find two or more , and save one of them in the group indexed as 1
  • , can match a comma that was not used by the previous regular expression, which means that it is a single comma

Replacing $1 uses a match from group 1

  • if we find,,,, we want to replace it with,, and such text will be placed in group 1
  • if we find only , then we want to replace it with nothing, and since the previous regular expression could not find its match, all its groups (which in our case are group 1) are also empty.
+1
source

All Articles