Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues with regex for overlapping matches
    primarykey
    data
    text
    <p>In short, I'm trying to match the longest item furthest right in a string that fits this pattern:</p> <pre><code>[0-9][0-9\s]*(\.|,)\s*[0-9]\s*[0-9] </code></pre> <p>Consider, for example, the string "abc 1.5 28.00". I want to match "5 28.00".</p> <p>Using the pattern "as-is", like so</p> <pre><code>preg_match_all('/[0-9][0-9\s]*(\.|,)\s*[0-9]\s*[0-9]/', 'abc 1.5 28.00', $result); </code></pre> <p>we instead get the following matches:</p> <pre><code>[0] =&gt; 1.5 2 [1] =&gt; 8.00 </code></pre> <p>No "5 28.00" or "28.00" for that matter, for obvious reasons.</p> <p>I did some research and people suggested using positive lookahead for problems like this. So I tried the following</p> <pre><code>preg_match_all('/(?=([0-9][0-9\s]*(\.|,)\s*[0-9]\s*[0-9]))/', 'abc 1.5 28.00', $result); </code></pre> <p>giving us these matches:</p> <pre><code>[0] =&gt; 1.5 2 [1] =&gt; 5 28.00 [2] =&gt; 28.00 [3] =&gt; 8.00 </code></pre> <p>Now, "5 28.00" is in there which is good, but it can't be reliably identified as the correct match (e.g. you can't just traverse from the end looking for the longest match, because there could be a longer match that appeared earlier in the string). Ideally, I'd want those sub-matches at the end (indexes 2 and 3) to not be there so we can just grab the last index.</p> <p>Does anyone have ideas for how to accomplish exactly what I need in the simplest/best way possible? Let me know if I need to clarify anything as I know this stuff can get confusing, and many thanks in advance.</p> <p>**Edit: some additional input/match examples</p> <p>"abc 1.5 28.00999" => "5 28.00" (i.e. can't match end of string, $)</p> <p>"abc 500000.05.00" => "5.00"</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. 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