fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.*;
  5.  
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. String s = "\"test\":[\"value1\", \"value2\"]";
  11. Pattern pattern = Pattern.compile("\"(test)\"\\s*:\\s*(?:\"([^\"]*)|\\[([^\\]]*))");
  12. Matcher matcher = pattern.matcher(s);
  13. while (matcher.find()){
  14. System.out.println("Key: " + matcher.group(1));
  15. if (matcher.group(2) != null) {
  16. System.out.println("Value: " + matcher.group(2));
  17. } else {
  18. System.out.println("Value: " + matcher.group(3));
  19. }
  20. }
  21. }
  22. }
Success #stdin #stdout 0.14s 36464KB
stdin
Standard input is empty
stdout
Key: test
Value: "value1", "value2"