0

I have a string like

String str = "1000,\"1123\",aabb,\"aa,bb\",test,\"abcd,\"";

I want to extract the substring by the delimiter comma ',' except for the comma which is marked with quotation marks. For the above example, I want to get the array is

1000 1123 abcd aa,bb test abcd,

I tried to use the regular expression, but I failed to find it. Please tell me how should I extract it. Many thanks.

user2256235
  • 275
  • 4
  • 14

1 Answers1

-1

(,\\"|\",)+ regex can do this what you want

str.replaceAll("(,\\\"|\\\",)+", "  ");

See it in action


Regex description:

1st Capturing group (,\\"|\",)+
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
1st Alternative: ,\\"
, matches the character , literally
\\ matches the character \ literally
" matches the characters " literally
2nd Alternative: \",
\" matches the character " literally
, matches the character , literally
g modifier: global. All matches (don't return on first match)
Manwal
  • 22,117
  • 10
  • 57
  • 89
  • It might be not worked for the string which end with the ," like "abc,\"abc,\",abc". Anyway, thanks for your reply – user2256235 Nov 03 '15 at 06:13