I need to remove \s*,\s* but only when it's not between ".
For example, this string a, b , c, "a, b , , c," a ,, should look like abc"a, b , , c," a.
As I found out [^abc] means don't touch abc and \.* means everything so I tried this:
str = str.replaceAll("[^\"\\.*,\\.*\"]\\s*,\\s*", "");
Important: amount of " is even.
bmargulies
101k40 gold badges196 silver badges329 bronze badges
1 Answer 1
Use this pattern
\s*,\s*(?=(?:(?:[^"]*"){2})*[^"]*$)
Demo
and I guess in your case, replace \s with \\s
\s*,\s* # your pattern
(?= # look-ahead
(?:(?:[^"]*"){2})* # optional pairs of double quotes
[^"]*$ # followed by optional anything but double quotes to the end
)
answered Dec 20, 2014 at 22:52
alpha bravo
7,9681 gold badge24 silver badges25 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
alpha bravo
explanation posted above.
Maksim
Thank you. You've helped me so much. =)
lang-java
\\.*means everything" - It actually will repeat the.character..means any symbol..literally.replaceAll("[^\".*,.*\"]\\s*,\\s*", "")It doesn't.a,b, "a, b?