2

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.

7
  • "\\.* means everything" - It actually will repeat the . character. Commented Dec 20, 2014 at 22:44
  • Are you sure? I thought . means any symbol. Commented Dec 20, 2014 at 22:45
  • 4
    It's escaped, so it will be . literally. Commented Dec 20, 2014 at 22:46
  • Do you mean it gonna work? replaceAll("[^\".*,.*\"]\\s*,\\s*", "") It doesn't. Commented Dec 20, 2014 at 22:49
  • Not very obvious with regexes; classical tools here would be better (indexOf etc). Also, what must be done if you have, say, a,b, "a, b? Commented Dec 20, 2014 at 22:54

1 Answer 1

5

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
)  
Sign up to request clarification or add additional context in comments.

2 Comments

explanation posted above.
Thank you. You've helped me so much. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.