Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think this is a difficult question to specify in a precise way, but perhaps this strategy will approximate your goal.</p> <p>The basic idea is to create a copy of the original string, removing the <strong>internally quoted items</strong>. An internally quoted item is defined here so that it must contains at least one non-whitespace character.</p> <p>After the internally quoted items have been removed, you then check whether the entire string needs surrounding quotes or not.</p> <pre><code>import re tests = [ # Test data in original question. ( '', '""' ), ( 'a', '"a"' ), ( '"a"', '"a"' ), # No change. ( '""a" b"', '""a" b"' ), # No change. ( '"a" b', '""a" b"' ), ( '"a" "b"', '""a" "b""' ), ( 'a "b" c', '"a "b" c"' ), # Test data in latest edits. ( 'type', '"type"' ), # Quote these. ( '"type" /?', '""type" /?"' ), ( '"type" "/?"', '""type" "/?""' ), ( 'type "a a" b', '"type "a a" b"' ), ( 'type "" b', '"type "" b"' ), ( '"type"', '"type"' ), # Don't quote. ( '""type" /?"', '""type" /?"' ), # Some more tests. ( '"a b" "c d"', '""a b" "c d""' ), ( '" a " foo " b "', '"" a " foo " b ""' ), ] Q = '"' re_quoted_items = re.compile(r'" \s* [^"\s] [^"]* \"', re.VERBOSE) for orig, expected in tests: # The orig string w/o the internally quoted items. woqi = re_quoted_items.sub('', orig) if len(orig) == 0: orig_quoted = Q + orig + Q elif len(woqi) &gt; 0 and not (woqi[0] == Q and woqi[-1] == Q): orig_quoted = Q + orig + Q else: orig_quoted = orig print orig_quoted == expected </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. 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.
    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