Regular expression for UK Mobile Number - Python

I need a regex that only checks UK mobile numbers .

A UK mobile number can be between 10-14 and either starts at 07 or skips 0 and starts at 447 .

It is important to note that if the user adds +44 , he must be rejected.

So they would be valid:

07111111111

447111111111

and this will be invalid:

+447111111111111

021929182711

00701029182


What I still have:

 rule = re.compile(r'^\+?(44)?(0|7)\d{9,13}$') if not rule.search(value): msg = u"Invalid mobile number." raise ValidationError(msg) 

This is not yet confirmed in accordance with my rules; can anyone help?

+4
source share
1 answer

The following regular expression looks like it will meet your requirements if I understand them correctly.

It is not possible to add a + sign, since you only create a white list of values, and plus is not among them.

^(07[\d]{8,12}|447[\d]{7,11})$

As mentioned in the comments on this answer, square brackets are not needed here. I turned them on to make my own reading of this regular expression a little easier before my eyes. However, the following works just as well:

^(07\d{8,12}|447\d{7,11})$

+8
source

Source: https://habr.com/ru/post/1479412/


All Articles