Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand what you're saying (and I'm not sure I do) you want to find the quoted string within your string ignoring escaped quotes. Is that right? If so, try this:</p> <pre><code>/(?&lt;!\\)'((?:\\'|[^'])*)(?&lt;!\\)'/ </code></pre> <p>Basically:</p> <ul> <li>Start with a single quote that isn't preceded by a backslash;</li> <li>Match zero or more occurrences of: backslash then quote or any character other than a quote;</li> <li>End in a quote;</li> <li>Don't group the middle parentheses (the ?: operator); and</li> <li>The closing quote can't be preceded by a backslash.</li> </ul> <p>Ok, I've tested this in Java (sorry that's more my schtick than Python but the principle is the same):</p> <pre><code>private final static String TESTS[] = { "'testing 123'", "'testing 123\\'", "'testing 123", "blah 'testing 123", "blah 'testing 123'", "blah 'testing 123' foo", "this 'is a \\' test'", "another \\' test 'testing \\' 123' \\' blah" }; public static void main(String args[]) { Pattern p = Pattern.compile("(?&lt;!\\\\)'((?:\\\\'|[^'])*)(?&lt;!\\\\)'"); for (String test : TESTS) { Matcher m = p.matcher(test); if (m.find()) { System.out.printf("%s =&gt; %s%n", test, m.group(1)); } else { System.out.printf("%s doesn't match%n", test); } } } </code></pre> <p>results:</p> <pre><code>'testing 123' =&gt; testing 123 'testing 123\' doesn't match 'testing 123 doesn't match blah 'testing 123 doesn't match blah 'testing 123' =&gt; testing 123 blah 'testing 123' foo =&gt; testing 123 this 'is a \' test' =&gt; is a \' test another \' test 'testing \' 123' \' blah =&gt; testing \' 123 </code></pre> <p>which seems correct.</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. 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