Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set maxlength in regex?

I have created this working regex below, this aims to accept phone numbers only ex. 09338195838.

^(09)[0-9]{9}

However I don't know how to set a maxlength that will accept only 11 digits. I've been to other similar questions like q1 but none really helped, and tried the below code but still it accepts even if it exceeds to 11 digits.

((09)[0-9]{9}){11}

Here is the detailed structure:

  1. must starts with 09
  2. must all numeric
  3. must 11 digits

Someone knows how to do this?

like image 922
charles Avatar asked Nov 25 '25 05:11

charles


2 Answers

^09[0-9]{9}$
The $ symbol means "The string has to end, without other characters after this".

like image 148
Zoldszemesostoros Avatar answered Nov 27 '25 20:11

Zoldszemesostoros


You can set word boundry.

((09)[0-9]{9})\b

test: https://regexr.com/51c40

if you want to make it more elastic, not only 11 digit

you can use ((09)\d{0,9})\b

{0,9} sets the min and max lengths, now it accepts maximum 11 digits starts with 09 and min 2 digits that is 09.

like image 29
Cotur Avatar answered Nov 27 '25 20:11

Cotur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!