PHP - Lookahead Not So Greedy
I need to match n occurrences of "." in a string following specific rules.
The number of occurrences found by the regex should correspond with the exact amount of "."'s in the string.
The rules a
1) every "." must be preceded by at least one digit from 1-9, and can be preceded by up to two other digits from 0-9 (basically, values from 1-999)
2) every "." must be followed by at least one digit from 1-9, and can be followed by up to two other digits from 0-9 (again, values from 1-999).
Sample valid strings a
1) "1.3.5.7.9"
2) "21.23.25.27.29"
Sample invalid strings a
1)"01.5.7.9" (first digit of the number preceding the dot cannot be a zero)
2) "1.05.7.9" (first digit of the number preceding or following the dot cannot be a zero)
3) "1.5.7777.9999" (cannot have more than 1 digit with value 1-9 and up to two more digits with value 0-9 (so up to 3 digit numbers); here we have four digit numbers)
I'm using preg_match_all to match all occurrences of the dot in the string, and I'm using lookaheads seeing that the digits matched are shared between the dots (the same digit that is preceding a dot is following another dot). The problem is, when I have two or three digit numbers, I'm getting more matches than I need.
Here is my pattern: /(?=([1-9][0-9]{0,2}\.[1-9][0-9]{0,2}))/
Here is my test subject: "Mt1,1-15.17.19"
There are only two dots, but I am getting four matches:
array (