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 = "[key1=value1, key2=value2, key 3= text,value # 3]";
  11. Pattern pattern = Pattern.compile("(\\w[^,=]*)=(.*?)(?=]|,\\s*\\w[^,=]*=)");
  12. Matcher matcher = pattern.matcher(s);
  13. while (matcher.find()){
  14. System.out.println("Key-value pair found:");
  15. System.out.println(matcher.group(1).trim());
  16. System.out.println(matcher.group(2).trim());
  17. }
  18. }
  19. }
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
Key-value pair found:
key1
value1
Key-value pair found:
key2
value2
Key-value pair found:
key 3
text,value # 3