Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Most of the answers seem massively over complicated. You <strong>don't</strong> need back references. You <strong>don't</strong> need to depend on whether or not re.findall gives overlapping matches. Given that the input cannot be parsed with the csv module so a regular expression is pretty well the only way to go, all you need is to call re.split with a pattern that matches a field.</p> <p>Note that it is much easier here to match a field than it is to match a separator:</p> <pre><code>import re data = """part 1;"this is ; part 2;";'this is ; part 3';part 4;this "is ; part" 5""" PATTERN = re.compile(r'''((?:[^;"']|"[^"]*"|'[^']*')+)''') print PATTERN.split(data)[1::2] </code></pre> <p>and the output is:</p> <pre><code>['part 1', '"this is ; part 2;"', "'this is ; part 3'", 'part 4', 'this "is ; part" 5'] </code></pre> <p>As Jean-Luc Nacif Coelho correctly points out this won't handle empty groups correctly. Depending on the situation that may or may not matter. If it does matter it may be possible to handle it by, for example, replacing <code>';;'</code> with <code>';&lt;marker&gt;;'</code> where <code>&lt;marker&gt;</code> would have to be some string (without semicolons) that you know does not appear in the data before the split. Also you need to restore the data after:</p> <pre><code>&gt;&gt;&gt; marker = ";!$%^&amp;;" &gt;&gt;&gt; [r.replace(marker[1:-1],'') for r in PATTERN.split("aaa;;aaa;'b;;b'".replace(';;', marker))[1::2]] ['aaa', '', 'aaa', "'b;;b'"] </code></pre> <p>However this is a kludge. Any better suggestions?</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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