Regular expression for UK Mobile Number - Python

2024/9/16 22:58:31

I need a regular expression that only validates UK mobile numbers.

A UK mobile number can be between 10-14 digits and either starts with 07, or omits the 0 and starts with 447.

Importantly, if the user adds +44 it should be rejected.

So these would be valid:

07111111111

447111111111

and these would be invalid:

+4471111111111

021929182711

00701029182


What I have so far:

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

This does not validate according to my rules yet; could someone help?

Answer

The following regex seems like it would fit your requirements, if I understand them correctly.

Not allowing a + sign is very easy as you're only creating a whitelist of values, and the plus isn't among them.

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

As was mentioned in the comments for this answer, the square brackets are not necessary here. I included them to make my own reading of this regex a little easier on my eyes. However, the following works just as well:

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

https://en.xdnf.cn/q/72410.html

Related Q&A

Iterate through all the rows in a table using python lxml xpath

This is the source code of the html page I want to extract data from.Webpage: http://gbgfotboll.se/information/?scr=table&ftid=51168 The table is at the bottom of the page <html><tab…

Django: Serializing a list of multiple, chained models

Given two different models, with the same parent base class. Is there any way, using either Django Rest Framework Serializers or serpy, to serialize a chained list containing instances of both the chil…

Formatting cells in Excel with Python

How do I format cells in Excel with python?In particular I need to change the font of several subsequent rows to be regular instead of bold.

What is the legality of scraping YouTube data? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about programming within the scope defined in the help center.Cl…

Numpy: fast calculations considering items neighbors and their position inside the array

I have 4 2D numpy arrays, called a, b, c, d, each of them made of n rows and m columns. What I need to do is giving to each element of b and d a value calculated as follows (pseudo-code):min_coords = m…

How to see all the databases and Tables in Databricks

i want to list all the tables in every database in Azure Databricks. so i want the output to look somewhat like this: Database | Table_name Database1 | Table_1 Database1 | Table_2 Database1 | Table_3 D…

How to get transparent background in window with PyGTK and PyCairo?

Ive been trying really hard to create a window with no decoration and a transparent background using PyGTK. I would then draw the content of the window with Cairo. But I cant get it to work.Ive tried a…

concurrent.futures.ThreadPoolExecutor doesnt print errors

I am trying to use concurrent.futures.ThreadPoolExecutor module to run a class method in parallel, the simplified version of my code is pretty much the following: class TestClass:def __init__(self, sec…

How to write a Dictionary to Excel in Python

I have the following dictionary in python that represents a From - To Distance Matrix.graph = {A:{A:0,B:6,C:INF,D:6,E:7},B:{A:INF,B:0,C:5,D:INF,E:INF},C:{A:INF,B:INF,C:0,D:9,E:3},D:{A:INF,B:INF,C:9,D:0…

How can I check pooled connections in SQLAlchemy before handing them off to my application code?

We have a slightly unreliable database server, for various reasons, and as a consequence sometimes the database connections used by my application vanish out from under it. The connections are SQLAlch…