how to write a regular expression to check whether a number is consisting only of 5 digits?
Share
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.
This regular expression should work nicely:
This will check if a string consists of only 5 numbers.
/is the delimiter. It is at the beginning and the end of a regular expression. (User-defined, you can use any character as delimiter).^is a start of string anchor.\dis a shorthand for[0-9], which is a character class matching only digits.{5}means repeat the last group or character5times.$is the end of string anchor./is the closing delimiter.If you want to make sure that the number doesn’t start with 0, you can use the following variant:
Where:
/is the delimiter. It is at the beginning and the end of a regular expression. (User-defined, you can use any character as delimiter).^is a start of string anchor.[1-9]is a character class matching digits ranging from1to9.\dis a shorthand for[0-9], which is a character class matching only digits.{4}means repeat the last group or character4times.$is the end of string anchor./is the closing delimiter.Note that using regular expressions for this kind of validation is far from being ideal.