How to easily parse all expressions inside double quotes from a string?

I have a line, something like ["first" "second" "third"].

Is there an easy way to get all expressions inside quotes as an array of String?

I know I can parse its char to char, but maybe there is an easier way to do this?

Surprisingly, google does not give me any solutions.

+4
source share
1 answer

This will be done:

String[] terms = str.replaceAll("^.*?\"|\"[^\"]*$", "").split("\"\\s+\"");

This works by first removing the first and last quotes and all the characters between them and the ends (via a call replaceAll(), and then splitting it into quote-whitespace-quote, leaving you only with the content you use.


Some test codes:

String str = "[\"first\" \"second\" \"third\"] ";
String[] terms = str.replaceAll("^.*?\"|\"[^\"]*$", "").split("\"\\s+\"");
System.out.println(Arrays.toString(terms));

Output:

[first, second, third]
+4

Source: https://habr.com/ru/post/1615402/


All Articles