Search code examples
preg-match-all

preg_match_all not completing the string match


I'm wanting to match all instances of needle in a haystack. My needle is:

/([0-9]{6,}) ([0-9]{10,}) ([0-9]{4,5}) (.{5,}) ([0-9]{1,}) ([0-9\.,]{4,8}) ([0-9\.,]{4,8})/gU

And my haystack is:

6292181 5702016627428 2304 WIDGET 18 14.12 254.16 6102211 5702015357180 10696 WIDGET 16 32.34 517.44 6332205 5702016911053 10946 WIDGET 6 32.36 194.16 

The problem I'm having is that the decimal place at the end of each match is not being included. So instead of matching

 6292181 5702016627428 2304 WIDGET 18 14.12 254.16 
 6102211 5702015357180 10696 WIDGET 16 32.34 517.44
 6332205 5702016911053 10946 WIDGET 6 32.36 194.16 

it's matching as

 6292181 5702016627428 2304 WIDGET 18 14.12 254 
 6102211 5702015357180 10696 WIDGET 16 32.34 517
 6332205 5702016911053 10946 WIDGET 6 32.36 194 

It seems to me there is an issue with the gU parameters.

Here's my workings: https://regex101.com/r/OgKqOV/1


Solution

  • Adding + to the last part will help. It will convert the pattern from lazy to posessive.

    So the regex becomes:

    ([0-9]{6,}) ([0-9]{10,}) ([0-9]{4,5}) (.{5,}) ([0-9]{1,}) ([0-9\.,]{4,8}) ([0-9\.,]{4,8}+)
    

    Working example (same as yours):

    https://regex101.com/r/h5gbjL/1