Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 Regex's but I need this one ASAP, but rest assured I won't post every time i need one. When i know what's going on I'll be answering some posts hopefully!

 

Any help is much appreciated.

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...