Problem with your current regex wiz r'^[0-9]{8,8}'
:
{8,8}
minimum and maximum length you want is 8 so you can make it exact 8 like{8}
, no need to have range- Current regex has condition to check beginning of string but you have not defined end of string which can be defined using symbol
$
which is allowing comma separated numbers so to avoid that you can simply add$
at the end in your current regex liker'^[0-9]{8}$'
and it will fix the issue. However, you can still optimize regex using\d
instead of providing range from[0-9]
as you’re using all the numbers so regex can be further simplified to"^[0-9]{8}$"
, also provided sample code below.
code:
import re
in_str = "48848484"
if re.match(pattern="^\d{8}$", string=in_str): print("number is correct")
else: print("wrong phone number")
0
solved Python 3 Regex 8 numbers only