Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use the magic of negative lookahead, to split your strings on a comma <strong>not followed by</strong> a digit use</p> <pre><code>public static void main(String[] args) { final String s = "{aaa=11,11,bbb=2222,ccc=333,3}"; final String[] ss = s.substring(1, s.length() -1).split(",(?!\\d)"); for(final String str : ss) { System.out.println(str); } } </code></pre> <p>Output</p> <pre><code>aaa=11,11 bbb=2222 ccc=333,3 </code></pre> <p>You can easily expand this to yank the key=value pairs directly</p> <pre><code>public static void main(String[] args) { final String s = "{aaa=11,11,bbb=2222,ccc=333,3}"; final Pattern p = Pattern.compile("([A-Za-z]++)=([\\d,]+)(?!\\d)[,}]"); final Matcher matcher = p.matcher(s); while (matcher.find()) { System.out.println(matcher.group(1)); System.out.println(matcher.group(2)); System.out.println("DONE"); } } </code></pre> <p>Output</p> <pre><code>aaa 11,11 DONE bbb 2222 DONE ccc 333,3 DONE </code></pre> <p><strong>EDIT</strong></p> <p>Following the OP's comment:</p> <p><em>The value part of the pair is alphanumeric (including <code>,+-*/=()</code>), also the value is always encapsulated in quotes, there could be multiple occurances of <code>,+-*/=()</code> too...</em></p> <p>I have revised the expression:</p> <pre><code>public static void main(String[] args) { final String s = "{1=\"1, one\", 22=\"+t,w,o\", 333=\"three, 3, -33,,333\", 4444=\"four. 4-4, (44), -44\"}"; System.out.println("String is: " + s); final Pattern p = Pattern.compile("([^{=,\\s]++)=\"([^\"]++)\""); final Matcher matcher = p.matcher(s); while (matcher.find()) { System.out.println(matcher.group(1)); System.out.println(matcher.group(2)); System.out.println("DONE"); } </code></pre> <p>Output:</p> <pre><code>String is: {1="1, one", 22="+t,w,o", 333="three, 3, -33,,333", 4444="four. 4-4, (44), -44"} 1 1, one DONE 22 +t,w,o DONE 333 three, 3, -33,,333 DONE 4444 four. 4-4, (44), -44 DONE </code></pre> <p>The pattern will now match anything that is not <code>=,{</code> or whitespace followed by an <code>=</code> and then followed by any pattern not containing <code>"</code> encapsulated in <code>"</code>. Does this help?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload