Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First thing you should use a CSV parsing library to parse the comma separated values. <strong>Correctly</strong> parsing CSV data isn't as trivial as it first seems. There are lots of <a href="http://jumpingbean.co.za/blogs/java-csv-file-parsing" rel="nofollow">good arguments</a> to not reinvent that wheel.</p> <p>This will also future proof your code and be code you don't have to test or maintain.</p> <p>I know the temptation to do something like <code>data.split(',');</code> is strong, but it is fragile and brittle solution. For just one example, what if any of the values contain the ','.</p> <p>Second thing you should do is then parse the pairs. Again the temptation to use <code>String.split("=");</code> will be strong, but it can be brittle and fragile if the right hand side of the <code>=</code> has an <code>=</code> in it. </p> <p>I am not a blind proponent of regular expressions, but used with restraint they can be just the right tool for the job. Here is the regular expression to parse the name value pairs.</p> <p>The regular expression <a href="http://www.rubular.com/r/UFOJBOEj9x" rel="nofollow">^(.*)\s?=\s?("?([^"]*)"?|"(.*)")$</a>, click the regular expression to test it online interactively. This works even for multiple double quotes in the right hand side of the name value pair.</p> <p>This will match only what is on the left side of the first <code>=</code> and everything else on the right hand side, and strip the optional <code>"</code> off the string values, while still matching the non-quoted number values.</p> <p>Given a <code>List&lt;String&gt; list</code> of the encoded name value pairs.</p> <pre><code>final Pattern p = Pattern.compile("^(.*)\s?=\s?("?([^"]*)"?|"(.*)")$"); final Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(list.size()); for (final String nvp : list) { final Matcher m = p.matcher(nvp); m.matches(); final String name = m.group(1); final String value = m.group(2); System.out.format("name = %s | value = %s\n", name, value); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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