Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly, please use <code>'</code> strings to write your regexes. That saves you a lot of escaping.</p> <p>Then I see two possibilities. The problem with your attempt is, it allows only consecutive escaped quotes in one place in the string. Also, this allows the use of different quotes at the beginning and the end. You could use a backreference to get around that. So this would be a) slightly more elegant and b) correct:</p> <pre><code>$pattern = '/(["\'])(\\"|\\\'|[^"\'])+\1/'; </code></pre> <p>Note that the order of the alternation is important!</p> <p>The problem with this is, you don't want to escape the quote that you don't use to delimit the string. Therefore, the other possibility is to use lookarounds (since backreferences cannot be used inside character classes):</p> <pre><code>$pattern = '/(["\'])(?:(?!\1).|(?&lt;=\\\\)\1)+\1/'; </code></pre> <p>Note that four consecutive backslashes are always necessary to match a single literal backslash. That is because in the actual string <code>$pattern</code> they end up as <code>\\</code> and then the regex engine "uses" the first one to escape the second one.</p> <p>This will match either an arbitrary character if it is <strong>not</strong> the starting quote. Or it will match the starting quote if the previous character was a backslash.</p> <p><a href="http://ideone.com/4iZnXd" rel="nofollow">Working demo.</a></p> <p>This by the way is equivalent to:</p> <pre><code>$pattern = '/(["\'])(?:\\\\\1|(?!\1).)+\1/'; </code></pre> <p>But here you <strong>have</strong> to write the alternation in this order again.</p> <p><a href="http://ideone.com/H9j6vB" rel="nofollow">Working demo.</a></p> <p>One final note. You can avoid the backreference by providing the two possible strings separately (single and double quoted strings):</p> <pre><code>$pattern = '/"(?:\\\\"|[^"])+"|\'(?:\\\\\'|[^\'])+\'/'; </code></pre> <p>But you said you were looking for something short and elegant ;) (although, this last one <strong>might</strong> be more efficient... but you'd have to profile that).</p> <p>Note that all my regexes leave one case unconsidered: escaped quotes outside of quoted strings. I.e. <code>Hello \" World "Hello" World</code> will give you <code>" World"</code>. You can avoid this using another negative lookbehind (using as an example the second regex for which I provided a working demo; it would work the same for all others):</p> <pre><code>$pattern = '/(?&lt;!\\\\)(["\'])(?:\\\\\1|(?!\1).)+\1/'; </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. 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