1

I've asked and it was answered but now, after years, it doesn't work. I've even tried online regex validators. Not sure what is going on.

Version: PHP 7.0.30 on 64Bit OS

The string should only allow digits with commas. No commas in the beginning or end. Spaces between commas is ok but I'd rather not allow it.

The following isn't passing

My regex is:

$DateInvoicedIDs = "1031,453,808,387,111,342,962,706,251,442,362,858,950,738,310,288,99,665,1023,30,894,112,132,148,347,895,382,94,766,683,276,1104,658,34,348,235,786,769,2";

$reg = '/[0-9\s]+(,[0-9\s]+)*[0-9]$/';
if ( preg_match($reg, $DateInvoicedIDs) ) {
  echo = $DateInvoicedIDs;
} else { echo "false"; }

I'm using preg_match and getting false.

Any idea?

1
  • Can the string end with whitespace, or only numbers? Maybe ^\d+[\d,\s]*(?<!,)$ if whitespace is allowed at the end. Commented Jun 26, 2018 at 0:28

3 Answers 3

1

Test your string and pattern @ https://regex101.com/r/3TVmOv/1

When that loads, you will see that there is no match highlighted.

Then add a digit to the end of your string and Whalla! This is because (,[0-9\s]+)* is matching the final 2 and [0-9]$ cannot be satisfied because another digit is required.

If I understand your logic/requirements, I think I'd use ~^\d+(?:\s*,\s*\d+)*$~

This improves the validation because it doesn't allow a mixture of digits and spaces between commas like: 2, 3 4 56, 72 I don't think you want spaces in your comma-separated numerical values.

Pattern Demo

Code: (Demo)

$DateInvoicedIDs = "1031,453,808,387,111,342,962,706,251,442,362,858,950,738,310,288,99,665,1023,30,894,112,132,148,347,895,382,94,766,683,276,1104,658,34,348,235,786,769,2";

$reg = '/^\d+(?:\s*,\s*\d+)*$/';
if (preg_match($reg, $DateInvoicedIDs)) {
    echo $DateInvoicedIDs;
} else {
    echo "false";
}
Sign up to request clarification or add additional context in comments.

Comments

0

It is not matching because of the last [0-9] in your regex. The * in (,[0-9\s]+)* is a greedy match which means that it is consuming all commas followed by digits in your string. There is nothing left after to match against the last [0-9].

So you probably want to reduce your regex to '/[0-9\s]+(,[0-9\s]+)*$/.

Comments

0

The last part of your regex [0-9]$ is what's causing it to fail:

[0-9\s]+ is matching the first number only 1031,

(,[0-9\s]+)* is covering everything until ,2 because it's a single number right after a comma which is what it's looking for

Then [0-9]$ is trying to find one more number but it can't

If the last number is a double-digit number, i.e. ,25 instead of 2, then the that second part (,[0-9\s]+)* would be satisfied because it found at least one number and [0-9]$ would match the next number which is 5 (https://regex101.com/r/0XbHsw/1)

Adding ? for that last part would solve the problem: [0-9\s]+(,[0-9\s]+)*[0-9]?$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.