So, I want to replace all & in the following string with a single &. These & can be separated by one or more spaces. I have a sample input string and a code snippet of what I have.
Input string:
a& & &&& & b
Here is what I have so far:
String foo = "a& & &&& & b".replaceAll("(\\s*&+\\s*&\\s*)", "&");
System.out.println(foo); // prints: "a&&b"
// expected: "a&b"
I’m not sure why two & are ending up in the result.
Any hlep would be appreciated. Thanks!
Try:
String foo = "a& & &&& & b".replaceAll("(\\s*&[\\s&]*)", "&");