smallB
smallB

Reputation: 17148

Regular expressions to match n digit number

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

Upvotes: 23

Views: 45462

Answers (3)

Joey
Joey

Reputation: 354874

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 $.

Upvotes: 28

rynkadink
rynkadink

Reputation: 138

I believe this will work for you. It matches strings with n numbers or strings like "(" or ")". Exmpl.: for n = 2 it matches strings like "12", "22" ....

^([0-9]{n})$|^([()]{1})$

Upvotes: 1

Polynomial
Polynomial

Reputation: 28346

This should work: [0-9\(\)]+

Upvotes: 19

Related Questions