Note that there are some explanatory texts on larger screens.

plurals
  1. POPreg Patterns, to ignore escaped characters
    primarykey
    data
    text
    <p>I want to create a RegEx that finds strings that begin and end in single or double quotes.</p> <p>For example I can match such a case like this:</p> <pre><code>String: "Hello World" RegEx: /[\"\'][^\"\']+[\"\']/ </code></pre> <p>However, the problem occurs when quotes appear in the string itself like so:</p> <pre><code>String: "Hello" World" </code></pre> <p>We know the above expression will not work.</p> <p>What I want to be able to do, it to have the escape within the string itself, since that will be functionality required anyway:</p> <pre><code>String: "Hello\" World" </code></pre> <p>Now I could come up with a long and complicated expression with various patterns in a group, one of them being:</p> <pre><code>RegEx: /[\"\'][^\"\']+(\\\"|\\\')+[^\"\']+[\"\']/ </code></pre> <p>However that to me seems excessive, and I think there may be a shorter and more elegant solution.</p> <p>Intended syntax:</p> <pre><code>run arg1 "arg1" "arg3 with \"" "\"arg4" "arg\"\"5" </code></pre> <p>As you can see, the quotes are really only used to make sure that string with spaces are counted as a single string. Do not worry about <code>arg1</code>, I should be able to match unquoted arguments.</p> <p><strong>I will make this easier, arguments can only be quoted using double-quotes. So i've taken single quotes out of the requirements of this question.</strong></p> <p>I have modified Rui Jarimba's example:</p> <pre><code>/(?&lt;=")(\\")*([^"]+((\\(\"))*[^"])+)((\\"")|")/ </code></pre> <p>This now accounts pretty well for most cases, however there is one final case that can defeat this:</p> <pre><code>run -a "arg3 \" p2" "\"sa\"mple\"\\" </code></pre> <p>The second argument end with <code>\\"</code> which is a conventional way in this case to allow a backslash at the end of a nested string, unfortunately the regex thinks this is an escaped quote since the pattern <code>\"</code> still exists at the end of the pattern.</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.
 

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