[Solved] Extract substring containing multiple double quote marks JAVA


Declare an array and then store the match results to that. ([^\"]*) captures any character but not of " zero or more times. () called capturing group which is used to capture the characters which are matched by the pattern present inside that group. Later we could refer those captured characters through back-referencing.

String s = "i am a sample string. \"name\":\"Alfred\",\"age\":\"95\",\"boss\":\"Batman\" end of sample";
Pattern regex = Pattern.compile("\"([^\"]*)\"");
ArrayList<String> allMatches = new ArrayList<String>();
Matcher matcher = regex.matcher(s);
while(matcher.find()){
        allMatches.add(matcher.group(1));
 }
System.out.println(allMatches);

Output:

[name, Alfred, age, 95, boss, Batman]

0

solved Extract substring containing multiple double quote marks JAVA