Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is one regular expression-based approach I have used in the past; it supports prefixes in combination with quoted strings.</p> <p>A more correct/robust/performant approach would involve writing a simple parser, however in most usage scenarios the time and effort associated with implementing and testing the parser would be vastly disproportionate to the gains.</p> <pre class="lang-cs prettyprint-override"><code>private static readonly Regex searchTermRegex = new Regex( @"^( \s* (?&lt;term&gt; ((?&lt;prefix&gt;[a-zA-Z][a-zA-Z0-9-_]*):)? (?&lt;termString&gt; (?&lt;quotedTerm&gt; (?&lt;quote&gt;['""]) ((\\\k&lt;quote&gt;)|((?!\k&lt;quote&gt;).))* \k&lt;quote&gt;? ) |(?&lt;simpleTerm&gt;[^\s]+) ) ) \s* )*$", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture ); private static void FindTerms(string s) { Console.WriteLine("[" + s + "]"); Match match = searchTermRegex.Match(s); foreach(Capture term in match.Groups["term"].Captures) { Console.WriteLine("term: " + term.Value); Capture prefix = null; foreach(Capture prefixMatch in match.Groups["prefix"].Captures) if(prefixMatch.Index &gt;= term.Index &amp;&amp; prefixMatch.Index &lt;= term.Index + term.Length) { prefix = prefixMatch; break; } if(null != prefix) Console.WriteLine("prefix: " + prefix.Value); Capture termString = null; foreach(Capture termStringMatch in match.Groups["termString"].Captures) if(termStringMatch.Index &gt;= term.Index &amp;&amp; termStringMatch.Index &lt;= term.Index + term.Length) { termString = termStringMatch; break; } Console.WriteLine("termString: " + termString.Value); } Console.WriteLine(); } public static void Main (string[] args) { FindTerms(@"two terms"); FindTerms(@"prefix:value"); FindTerms(@"some:""quoted term"""); FindTerms(@"firstname:Jack ""the Ripper"""); FindTerms(@"'quoted term\'s escaped quotes'"); FindTerms(@"""unterminated quoted string"); } </code></pre> <p>Output:</p> <pre class="lang-none prettyprint-override"><code>[two terms] term: two termString: two term: terms termString: terms [prefix:value] term: prefix:value prefix: prefix termString: value [some:"quoted term"] term: some:"quoted term" prefix: some termString: "quoted term" [firstname:Jack "the Ripper"] term: firstname:Jack prefix: firstname termString: Jack term: "the Ripper" termString: "the Ripper" ['quoted term\'s escaped quotes'] term: 'quoted term\'s escaped quotes' termString: 'quoted term\'s escaped quotes' ["unterminated quoted string] term: "unterminated quoted string termString: "unterminated quoted string </code></pre>
 

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