I need a regex that allows numbers and optional commas, but the entire length cannot be greater than 6.
^[0-9]+([,]*[0-9]+)*$ allows numbers and optional commas.
^([0-9]+([,]*[0-9]+)*){0,6}$ does not limit the total length to 6.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If your regex engine supports lookahead assertions — most do — then you can write:
The
(?=[0-9,]{1,6}$)part is a “positive lookahead assertion”, and means “looking forward from this point in the string, I see[0-9,]{1,6}$“. So, in essence, the above regex is a combination of these two:and enforces them both.
(That said, it’s likely to be clearer if you simply enforce the length restriction as a separate step, rather than incorporating the above into a single regex.)