Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>$s = "Hello people #RegularExpression sucks!"; preg_match_all('~(?=(\S+\s+\S+))\S+\s+~', $s, $matches); print_r($matches[1]); </code></pre> <p>output:</p> <pre class="lang-none prettyprint-override"><code>Array ( [0] =&gt; Hello people [1] =&gt; people #RegularExpression [2] =&gt; #RegularExpression sucks! ) </code></pre> <p>explanation:</p> <p><code>\S+</code> matches one or more non-whitespace characters. Your <code>\w</code> was incorrect for two reasons: it only only matches one character; and it only matches a so-called <em>word character</em> (equivalent to <code>[A-Za-z0-9_]</code>). Adding the <code>+</code> to your <code>\s</code> wasn't necessary in this test case, but there's no reason <em>not</em> to add it, and extra whitespace does have a way of sneaking into text in the real world. (But be sure and add <code>+</code>, not <code>*</code>; there must be at least one whitespace character in there.)</p> <p><code>(?=...)</code> is a <a href="http://www.regular-expressions.info/lookaround.html" rel="nofollow">positive lookahead</a>. You use them to check whether it's <em>possible</em> to match the enclosed subexpression at the current match position, without <em>advancing</em> the match position. Then, typically, you go ahead and match a different subexpression, not in a lookahead.</p> <p>Here's the tricky bit: Although the characters matched by the lookahead subexpression are not <em>consumed</em>, any capturing groups in the subexpression work as usual. The lookahead in my regex, <code>(?=(\S+\s+\S+))</code> matches <em>and captures</em> the next two-word sequence. Then (assuming the lookahead succeeded) <code>\S+\s+</code> matches in the normal way, setting the match position correctly for the next attempt.</p> <p>This technique should work in any regex flavor that supports capturing groups and lookaheads. That includes PHP as well as all the other major languages (Perl, JavaScript, .NET, Python, Java...). The technique for accessing only the contents of the first capturing group from each match varies wildly from one language to the next, but PHP makes it easy, with <code>$matches[1]</code>.</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