Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to do it with regex, how about this?</p> <pre><code>(?&lt;=^|,)("[^"]*"|[^,]*)(?=,|$) </code></pre> <p>This matches comma-separated fields, including the possibility of commas appearing inside quoted strings like <code>123,"Yes, No"</code>. <a href="http://regexr.com?30q34" rel="nofollow noreferrer">Regexr for this.</a></p> <p>More verbosely:</p> <pre><code>(?&lt;=^|,) # Must be preceded by start-of-line or comma ( "[^"]*"| # A quote, followed by a bunch of non-quotes, followed by quote, OR [^,]* # OR anything until the next comma ) (?=,|$) # Must end with comma or end-of-line </code></pre> <p>Usage would be with something like Python's <code>re.findall()</code>, which returns all non-overlapping matches in the string (working from left to right, if that matters.) Don't use it with your equivalent of <code>re.search()</code> or <code>re.match()</code> which only return the first match found.</p> <p>(NOTE: This actually doesn't work in Python because the lookbehind <code>(?&lt;=^|,)</code> isn't fixed width. Grr. Open to suggestions on this one.)</p> <hr> <p>Edit: Use a non-capturing group to consume start-of-line or comma, instead of a lookbehind, and it works in Python.</p> <pre><code>&gt;&gt;&gt; test_str = '123,456,"String","String, with, commas","Zero-width fields next",,"",nyet,123' &gt;&gt;&gt; m = re.findall('(?:^|,)("[^"]*"|[^,]*)(?=,|$)',test_str) &gt;&gt;&gt; m ['123', '456', '"String"', '"String, with, commas"', '"Zero-width fields next"', '', '""', 'nyet', '123'] </code></pre> <hr> <p>Edit 2: The <a href="https://stackoverflow.com/questions/80357/ruby-match-all-occurrences-of-a-regex">Ruby equivalent</a> of Python's <code>re.findall(needle, haystack)</code> is <code>haystack.scan(needle)</code>.</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. This table or related slice is empty.
    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