$re = '/(?<!\d,)(?<!\d)\d{1,3}(?:,\d{3})*(?!,?\d)/m';
$str = 'I need help finding a regex rule that should search in a large string/text and match numbers that have the format: 12,345,678 or 1,234,567 or 12,345 or 1,234.
for example: for 12,345,678 it should match 12,345,678 and not 345,678 or 45,678 or anything similar
I looked in: Regex help needed to match numbers but the answers either match 1 in 1,23,456 (should not at all because 1,23,456 is not a number) or match 23,456 in 12,23,456 (should not match at all)
In creating a regex rule to match the correct format number, I tried first creating the rule of what it should not match(i.e., not 1,23,456), then I tried creating the rule of what it should match. The last rule I created matches in most cases, but not in all.
MATCH:
1 12 123 1,234 12,345 123,456 1,234,567 12,
NO MATCH:
1,24,567 1,234,5 1,2 1,2,567 12567 ';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php