Regex for South African legal phone number?

Kane

New member
Joined
Jun 30, 2004
Messages
1
Hi,

I need to make a regular expression that allows the following:

+27 (041) 5839999 or 27 041 5839999

Basically, an optional +, followed by any number of digits, followed by a space, optional bracket, any number of digits, optional bracket, space, any number of digits.

Also need one for:

+27 (083|082)5081744

Or, optional plus, then a space, then a 10 digit number starting with 082 or 083.

This is for a legal South African phone number.

I AM learning Regexs but I need this one ASAP, but rest assured I wont post every time i need one. When i know whats going on Ill be answering some posts hopefully!

Any help is much appreciated.
 
This should work.

"\+?[0-9]* \(?[0-9]*\)? [0-9]*"

Some things that might help you.
\ - means ignore an special meaning the character may have to the regular Expression. i.e. + normally means to search for 1 or more occurrence of something.

[ and ] - this looks for any single occurrence of the character with in the square brackets. In this case 0-9

* - looks for 0 or more occurrence of the proceeding character. so [0-9]* looks for 0 or more numbers. Note I find using * can be dangerous.

? - means to look for 0 or 1 occurrence of the character before. You can also use {0,1}. The curley brackets mean you can specify a range of occurrences you are looking for or an exact number {2}. You may find this useful to specify exactly how many numbers you are expecting and replace the * above. To make the pattern a bit more reliable

Hope this helps.
 
Back
Top