Use regex to remove unwanted commas

I am creating an application in which I created a line as follows:

 hihibjj,,,,,,,ghjjjj, , ,kartik.isworking@gmail.com,,,,karan.adep@gmail.com, android-developer-support@google.com, prerna.mungekar@hamarisuraksha.com

I want to remove unnecessary commas, only separate values ​​should be present, separated by a comma.

The code I tried

for (int i = 0; i < temp.length; i++) {
  for (int j = 0; j < emailSeperated.size(); j++) {
    if (temp[i].trim().equals(emailSeperated.get(j).trim())) {
      strEmailValue = strEmailValue.replace(temp[i], "").trim();
      Log.e("strEmail trimmed value", strEmailValue);
    } else if (temp[i].trim().equals(emailSeperated.get(j).trim())) {
      strEmailValue = strEmailValue.replaceAll(temp[i] + ",", "").trim();
    }
  }
} 
+4
source share
3 answers

You can also use lookbehind.

Regex:

(?<=,) ?,

Replaced string:

Empty string

Demo

System.out.println("hihibjj,,,,,,,ghjjjj, , ,kartik.isworking@gmail.com,,,,karan.adep@gmail.com, android-developer-support@google.com, prerna.mungekar@hamarisuraksha.com".replaceAll("(?<=,) ?,", ""));

Conclusion:

hihibjj,ghjjjj,kartik.isworking@gmail.com, karan.adep@gmail.com, android-developer-support@google.com, prerna.mungekar@hamarisuraksha.com 
0
source

Just find: ,,+and replace with:,

+7
source
String as = "as,,,,,,";
as= as.replaceAll(",,+",",");
0
source

All Articles