Regex of numbers and commas in correct fomat for PHP app
I need a regex for my php app that confirms that the user entered text in the format of numbers separated with commas. For example:
1,2
1,2,3
6,4,5,3,2,1
There can be no other characters but numbers and com开发者_开发知识库mas.
Thank you, Mark
This regular expression should do it: (\d+,)*\d+
.
Explanation:
\d+ One or more consecutive digits
\d+, ...followed by a comma
(\d+,)* ...and all of the above repeated zero or more times
(\d+,)*\d+ ...and finally ending with one or more digits
This expression will not allow an "extra" comma after the last number, or before the first number, or multiple consecutive commas.
I believe this is the regular expression you are looking for: \d+(,\d+)*
.
It has the advantage of matching a single number without a comma, or a comma-separated list of numbers with no trailing comma.
精彩评论