Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that <code>^</code> and <code>$</code> inside character classes are not treated as anchors but as literal characters. You could simply use alternation instead of a character class:</p> <pre><code>string pattern = string.Format(@"(?:\s|^){0}(?:\s|$)",keyword); </code></pre> <p>Note that there is no need for the <code>+</code>, because you just want to make sure if there is one space. You don't care if there are more of them. The <code>?:</code> is just good practice and suppresses <a href="http://www.regular-expressions.info/brackets.html" rel="nofollow">capturing</a> which you don't need here. And the <code>@</code> makes the string a verbatim string, where you don't have to double-escape your backslashes.</p> <p>There is another way, which I find slightly neater. You can use <a href="http://www.regular-expressions.info/lookaround.html" rel="nofollow">lookarounds</a>, to ensure that there is not a non-space character to left and right of your keyword (yes, double negation, think about it). This assumption is valid if there is a space-character or if there is one end of the string:</p> <pre><code>string pattern = string.Format(@"(?&lt;!\S){0}(?!\S)",keyword); </code></pre> <p>This does exactly the same, but might be slightly more efficient (you'd have to profile that to be certain, though - if it even matters).</p> <p>You can also use the first pattern (with non-inverted logic) with (positive) lookarounds:</p> <pre><code>string pattern = string.Format(@"(?&lt;=\s|^){0}(?=\s|$)",keyword); </code></pre> <p>However, this doesn't really make a difference to the first pattern, unless you want to find multiple matches in a string.</p> <p>By the way, if your <code>keyword</code> might contain regex meta-characters (like <code>|</code>, <code>$</code>, <code>+</code> and so on), make sure to escape it first using <a href="http://msdn.microsoft.com/en-GB/library/system.text.regularexpressions.regex.escape.aspx" rel="nofollow"><code>Regex.Escape</code></a></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. 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