Note that there are some explanatory texts on larger screens.

plurals
  1. POJava regular expression - cater for an inclusive comma
    text
    copied!<p>I'm trying to split a comma delimited pairs string but can't work out how to cater for an inclusive comma.</p> <p>Here is my test case -</p> <pre><code>private void stringSplit() { String value = "{aaa=1111,bbb=2222,ccc=3333}"; String regEx = "[^,]+=[^,]+"; String separator = "="; final Pattern pattern = Pattern.compile(regEx); final Matcher matcher = pattern.matcher(value); while (matcher.find()) { final String group = matcher.group(); final String key = group.substring(0, group.indexOf(separator)); final String val = group.substring( group.indexOf( separator ) + separator.length(), group.length()); System.out.println("key [" + key + "], val [" + val + "]"); } } </code></pre> <p>and here are my results -</p> <pre><code>key [{aaa], val [1111] key [bbb], val [2222] key [ccc], val [3333}] </code></pre> <p>All good so far...</p> <p>But there may be a comma in the numeric value i.e.</p> <pre><code>"{aaa=11,11,bbb=2222,ccc=333,3}"; </code></pre> <p>the results I would want are -</p> <pre><code>key [{aaa], val [11,11] key [bbb], val [2222] key [ccc], val [333,3}] </code></pre> <p>Could any of you regular expression guru's help me out here.</p> <p>thanks!</p> <p><strong>EDIT</strong></p> <p>Following on from @bmorris591 further comments.</p> <p>Ok, I have a final query - and this is a definitive list of what this crazy regex (+ a bit of java code) needs to handle.</p> <p>Here is my code -</p> <pre><code>private void stringSplit() { String value = "{1=\"1, one\", 22=\"+t,w,o\", 333=\"three, \"3\", -33,,333,\", 4444=\"four. '4-4, (44), -44\"}, 555=\"\", \"666\"=6666, \"777\"=\"7777\"}"; String regex = "[^\\{,]+=([[\\w]\\(\\)\\-\\+\\.\"'\\s,]+)[,}]"; String separator = "="; final Pattern pattern = Pattern.compile(regex); final Matcher matcher = pattern.matcher(value); while (matcher.find()) { final String group = matcher.group(); showKeyAndValue(group, separator); } } private void showKeyAndValue(final String group, final String keyValueSeparator) { System.out.println("group [" + group + "]"); final String key = removeQuotesFromString(group.substring(0, group.indexOf(keyValueSeparator))); final String val = removeQuotesFromString(group.substring( group.indexOf(keyValueSeparator) + keyValueSeparator.length(), group.length())); System.out.println("key [" + key + "], val [" + val + "]"); } private String removeQuotesFromString(final String str) { String returnString = str.trim(); if (returnString.startsWith("\"")) { returnString = returnString.substring( returnString.indexOf("\"") + 1, returnString .lastIndexOf("\"")); } return returnString; } </code></pre> <p>And here are the results -</p> <pre><code>group [1="1, one",] key [1], val [1, one] group [ 22="+t,w,o",] key [22], val [+t,w,o] group [ 333="three, "3", -33,,333,",] key [333], val [three, "3", -33,,333,] group [ 4444="four. '4-4, (44), -44"}] key [4444], val [four. '4-4, (44), -44] group [ 555="",] key [555], val [] group [ "666"=6666,] key [666], val [6666,] group [ "777"="7777"}] key [777], val [7777] </code></pre> <p>All results are correct <strong>apart</strong> from key 666. As you can see there is a trailing comma. Now I could just strip this off (for a <em>value</em> that is <strong>not</strong> encapsulated in quotes (basically a number)) but I was wondering if this could be acheived in the regex as this would be a 'cleaner' solution...</p> <p>Many, many thanks if you can think of anything.</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