Skip to content

Instantly share code, notes, and snippets.

@morrismukiri
Last active September 30, 2026 12:57
Show Gist options
  • Select an option

  • Save morrismukiri/920494322a47d9485bbc to your computer and use it in GitHub Desktop.

Select an option

Save morrismukiri/920494322a47d9485bbc to your computer and use it in GitHub Desktop.
Kenyan phone number regular expression
@MMakali

MMakali commented Jun 3, 2021

Copy link
Copy Markdown

If you want to validate +2547, or +2541, or just 07 or 01,
Follow this link for number prefixes in Kenya
^(+254|0)([7][0-9]|[1][0-1]){1}[0-9]{1}[0-9]{6}$

@chweyakevin

Copy link
Copy Markdown

@Code-Buddie how about this one to support 07xxx and 01xx numbers ^(+254|0)([17][0-9]|[1][0-1]){1}[0-9]{1}[0-9]{6}$

@MMakali

MMakali commented Sep 22, 2021

Copy link
Copy Markdown

@Code-Buddie how about this one to support 07xxx and 01xx numbers ^(+254|0)([17][0-9]|[1][0-1]){1}[0-9]{1}[0-9]{6}$

it still works
RegExp regExp = new RegExp(
r"^(+254|0)([7][0-9]|[1][0-1]){1}[0-9]{1}[0-9]{6}$",
caseSensitive: false,
multiLine: false,
);

@LukeNgirubiu

LukeNgirubiu commented Nov 23, 2023 •

Copy link
Copy Markdown

The python code snippet below validates a kenyan mobile phone number (For all service providers). The following 8 formats are valid. Any other format is invalid.

  1. 07XXXXXXXX
  2. 2547XXXXXXXX
  3. 7XXXXXXXX
  4. +2547XXXXXXXX
  5. 01XXXXXXXX
  6. 1XXXXXXXX
  7. +2541XXXXXXXX
  8. 2541XXXXXXXX
    validating_kenyan_number
    If the value of valid is None, then the mobile number provided doesn't match any of the formats listed above.

@Steven-zion

Copy link
Copy Markdown

right now we have number starting with both 07 and 01, what's the regex for that?

@LukeNgirubiu

LukeNgirubiu commented Apr 19, 2024 via email

Copy link
Copy Markdown

@MMakali

MMakali commented Apr 19, 2024

Copy link
Copy Markdown

`import re

pattern = r'^(+254|0)([7][0-9]|[1][0-1]){1}[0-9]{1}[0-9]{6}$'

Test the pattern

phone_numbers = [
"+254712345678",
"0712345678",
"0112345678"
]

for number in phone_numbers:
if re.match(pattern, number):
print(f"{number} is a valid phone number.")
else:
print(f"{number} is not a valid phone number.")
`

@Steven-zion

Copy link
Copy Markdown

the phone input am using holds the phone to state in the format +254 712 345678, it has refused to pass most regex. can it be the spacing. please provide me with a regex that validates this exactly as it is, thanks

@LukeNgirubiu

LukeNgirubiu commented May 4, 2024 via email

Copy link
Copy Markdown

@ElzaCremin

ElzaCremin commented May 15, 2024 •

Copy link
Copy Markdown

Can I also ask a question because I have some doubts about it? Last semester, I was under immense pressure to finish my dissertation. I found https://ukwritings.com/write-my-dissertation and they came to my rescue. Their writers are experts in various fields, and they delivered a well-structured dissertation on time. The service was worth every penny, and I couldn't be happier with the result.

@obbyK

obbyK commented Jun 14, 2024

Copy link
Copy Markdown

Works with Java, but you can probably simplify them further.

Safaricom (\\+?254|0)?([7][0129][0-9]|[7][4][0123568]|[7][5][789]|[7][6][89]|[1][1][012345]){1}[0-9]{6}
Airtel (\\+?254|0)?([7][38][0-9]|[7][5][0123456]|[7][6][2]|[1][0][012345678]){1}[0-9]{6}
Telkom (\\+?254|0)?(77[0-9]){1}[0-9]{6}
Faiba (\\+?254|0)?(747){1}[0-9]{6}
Equitel (\\+?254|0)?(76[3456]){1}[0-9]{6}

@Bradleykingz

Copy link
Copy Markdown

(+?2547\d{8}|07\d{8}|+?25411\d{7}|011\d{7})

@domyassignmentshelper

Copy link
Copy Markdown

Finance assignments often require accurate calculations, financial analysis, relevant research, and clear academic writing. Students handling challenging coursework may find Finance Assignment Help UK useful for understanding complex concepts, organising their ideas, reviewing drafts, and improving the overall clarity of their academic work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment