Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is one of many situations where lookbehind seems like the obvious tool for the job, but you can't use it. The main problem is that a lookbehind subexpression can't use quantifiers like <code>?</code>, <code>*</code>, <code>+</code>, or <code>{min,max}</code>, so this won't work:</p> <pre><code>preg_match('~(?&lt;!//.*)\bthis\b~', $subject, $match); // nope! </code></pre> <p>In most regex flavors you would have to match the whole line with a regex that doesn't allow <code>//</code> to appear anywhere before the token you're looking for. @Gumbo showed one way to do that, and here's another:</p> <pre><code>preg_match('~^(?:(?!//).)*\bthis\b~', $subject, $match); </code></pre> <p>If your goal is to replace the token with another--for example, replacing <code>this</code> with <code>that</code>--you'll have to capture the stuff before it and plug it back in:</p> <pre><code>preg_replace('~^((?:(?!//).)*)\bthis\b~', '$1that', $subject); </code></pre> <p>However, PHP offers another option: <code>\K</code>, Match Point Reset operator (or as I like to call it, "Poor Man's Lookbehind"). </p> <pre><code>preg_match('~^(?:(?!//).)*\K\bthis\b~', $subject, $match); </code></pre> <p>You match the preceding matter in the usual way, but the <code>\K</code> tells the regex engine to <em>pretend</em> the match really started just before the <code>\bthis\b</code>, not at the beginning of the string, so the value of <code>$match[0]</code> is simply <code>"this"</code>. If you're doing a replace, there's no need to capture the preceding matter and plug it back in:</p> <pre><code>preg_replace('~^(?:(?!//).)*\K\bthis\b~', 'that', $subject); </code></pre> <p><code>\K</code> originated in the Perl regex flavor.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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