Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a regex that allows 9 or 10 digits

Tags:

regex

I need help with a regex

I need it to match either a 9 or 10 digit value that starts with 50.

I have:

^[ ]*(50)[0-9]{7}[ ]*$

which allows 9 digits.

How can I expand this so that it also allows 10 digits?

like image 732
raklos Avatar asked Feb 03 '26 17:02

raklos


2 Answers

Add the range {7,8}

^[ ]*(50)[0-9]{7,8}[ ]*$

FYI this site describes the standard quantifiers that you can use in a regular expression:

* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
like image 76
psxls Avatar answered Feb 06 '26 09:02

psxls


Try with following regex:

^[ ]*50\d{7,8}[ ]*$
like image 27
hsz Avatar answered Feb 06 '26 08:02

hsz