Extract values from CSV-like string including empty fields with PCRE regex
<p>I try to capture values of columns including(!) empty columns from multiple csv-like simple data column strings, seperated by a semicolon. And even if I know that regex isn't the best approach for that and explicit csv parsers would do a quite better job, in this case I have no other choice than to use PRCE regex to build html table <code>&lt;td&gt;</code> groups from this data.</p>

<p>The worsed example which should (still) work, looks like this:</p>

<pre><code>;testvalue;"testvalue";"test "val"ue";test value;
</code></pre>

<p>... which should be literally interpreted like this:</p>

<pre><code>empty | testvalue | testvalue | test "val"ue | test value | empty
</code></pre>

<p>... which gets finally rendered into this (not part of the question):</p>

<pre><code>&lt;td&gt;empty&lt;/td&gt;
&lt;td&gt;testvalue&lt;/td&gt;
&lt;td&gt;testvalue&lt;/td&gt;
&lt;td&gt;test"val"ue&lt;/td&gt;
&lt;td&gt;test value&lt;/td&gt;
&lt;td&gt;empty&lt;/td&gt;
</code></pre>

<p>(UPDATE like asked of @anubhava) </p>

<p>Sadly there is another <em>downer</em> coming with it: The system where it will be implemented has a fixed way to handle the strings. It will ONLY recognize and ONLY alter captured groups of the string. Any other unregistered part of the string gets directly printed out with the rest <em>as-is</em>. That means: we need to <strong>register</strong> the semicolons in the regex, even if we don't want them to be printed out, but to remove them by ignoring their matching group.</p>

<p>Usually, it would be enough to only print out the captured group, but this doesn't work here. To ONLY capture the values would cause this output:</p>

<pre><code>;;;;;
&lt;td&gt;empty&lt;/td&gt;
&lt;td&gt;testvalue&lt;/td&gt;
&lt;td&gt;testvalue&lt;/td&gt;
&lt;td&gt;test"val"ue&lt;/td&gt;
&lt;td&gt;test value&lt;/td&gt;
&lt;td&gt;empty&lt;/td&gt;
</code></pre>

<p><em>Maybe we need to capture the whole string first in another group or we need to capture the semicolons in another group to throw them away later in the print out?</em> ...</p>