Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expressions to match n digit number

Tags:

regex

Could someone tell me what the regular expression would be to match: number with n digits where n would be provided or ( or )

like image 212
smallB Avatar asked Nov 03 '11 17:11

smallB


People also ask

How do you match a regular expression with digits?

To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.

How does regex Match 5 digits?

match(/(\d{5})/g);

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.


1 Answers

Simple:

\(|\)|\d{n}

replace n with the number of digits you need. If you need to match a complete string, then put parentheses around the expression and prepend ^ and append $.

like image 93
Joey Avatar answered Sep 23 '22 17:09

Joey