A Regular Expression that matches Nigerian Phone Numbers.
For the benefit of those developers who are in the middle of writing code that will save the world and just need to copy and paste a regular expression that will validate Nigerian phone numbers, so that they can get back to saving the world as soon as possible, here is your TL, DR:
((^+)(234){1}[0–9]{10})|((^234)[0–9]{10})|((^0)(7|8|9){1}(0|1){1}[0–9]{8})
For the rest of us who can afford to wait around, welcome to my keynote speech!😃.
Recently, I needed to implement validation for Nigerian phone numbers. I ran a very quick google search for a regular expression I could use. Surprisingly, I did not find any that solved my problem. So, a task that I thought would be a simple CTRL+C followed by a CTRL+V on a beautiful Saturday morning resulted in me trying to put the regular expression together myself. Sigh! It was supposed to be frustrating but I enjoyed the process. Why did I enjoy it? I think every programmer just finds delight in solving problems, even on a Saturday morning.
To begin, here is a poem that I love — The Bridge Builder, Will Allen Dromgoole.
Personalizing the words of the old man in the poem:
“In the path I have come
There follows after me to-day
Another developer whose feet must pass this way.
This chasm that has been as naught to me
To that fine developer may a pitfall be;
He, too, must cross in the twilight dim;
For him, I am building this bridge.”
All right. Enough playing around. Let’s get started. Here are three variants of a Nigerian phone number: +234803XXXXXXX, 234803XXXXXXX and 0803XXXXXXX.
Well, those are the three parts the regex is divided into:
(1.)… ((^+)(234){1}[0–9]{10}) validates +234803XXXXXXX
(2.)… ((^234)[0–9]{10}) validates +234803XXXXXXX
(3.)…((^0)(7|8|9){1}(0|1){1}[0–9]{8})) validates 0803XXXXXXX
Let us focus on the first part:
((^+)(234){1}[0–9]{10})
This expression matches:
(^+) any text that begins with a “+”, followed by
(234){1} only one occurrence of the exact text “234”, followed by
[0–9]{10} ten occurrences of any digit from 0 to 9.
And the second part:
((^234)[0–9]{10})
This expression matches:
(^234) any expression starting with the exact text “234”, followed by
[0–9]{10} ten occurrences of any digit from 0 to 9.
Finally:
((^0)(7|8|9){1}(0|1){1}[0–9]{8}))
this expressions matches:
(^0) any text that begins with a “0”, followed by
(7|8|9){1} exactly one occurrence of any of the text “7”, “8” or “9”, followed by
(0|1){1} exactly one occurrence of any of the text “0” or “1”, followed by
[0–9]{8} eight occurrences of any digit from 0 to 9.
The regular expression we have discussed, will validate Nigerian phone numbers from the popular carriers in whatever form they are written.
I have a confession to make. While putting together this solution, I wanted to get back to writing my code as soon as possible. Once it passed my test cases, I abandoned it. So, I am sure that the regular expression can be simplified further. I hope to read your comments. Ciao!