I’m not an experienced RegEx user, but searched through net and found different patterns and made some modifications to adapt my needs
But when I put them together to 1 compound expression it fails, and I cant find the reason why.
My RegEx is tested in Excel with VBA
I want to test if a phone number has the correct format, and it might have area code.
First i made an solution with following statement:
oRegEx.Pattern = “^s*((?+[0-9]{1-3})?)?s*”
bReturn = oRegEx.Test(sNumber) The sNumber might be ” +47 ” or ” (+47) ” or even ” ” (blank string w spaces)
All of them give True on the test
Then I tested the rest of the phone number with the statement:
oRegEx.Pattern = “^s*(?:s*[0-9]{2}){4}s*”
bReturn = oRegEx.Test(sNumber) is tested with sNumber = ” 12345678 ” and sNumber = ” 12 34 56 78 ”
Those works as well
But when i try to make one statement of them, the test Fails, and my knowledge is to limited to figure out why it doesn’t work
oRegEx.Pattern = “^s*((?+[0-9]{1-3})?)?s*?(?:s*[0-9]{2}){4}s*”
sNumber = ” +47 12345678 “
I think this has to do with the characters between the two statements, I make it optional to ‘eat spaces’
Added the spaces just for testing, should probably Trim before sending to my RegEx function
Here is the complete tests I’ve done, All testes work except the last one that fails
oRegEx.Pattern = "^s*((?+[0-9]{1-3})?)?s*"
sNumber = " +47 "
bReturn = oRegEx.Test(sNumber)
oRegEx.Pattern = "^s*((?+[0-9]{1-3})?)?s*"
sNumber = " (+47) "
bReturn = oRegEx.Test(sNumber)
oRegEx.Pattern = "^s*((?+[0-9]{1-3})?)?s*"
sNumber = " "
bReturn = oRegEx.Test(sNumber)
oRegEx.Pattern = "^s*(?:s[0-9]{2}){4}s*"
sNumber = " 12 34 56 78 "
bReturn = oRegEx.Test(sNumber)
oRegEx.Pattern = "^s*(?:s*[0-9]{2}){4}s*"
sNumber = " 12345678 "
bReturn = oRegEx.Test(sNumber)
bReturn = False
oRegEx.Pattern = "^s*((?+[0-9]{1-3})?)?s*?(?:s*[0-9]{2}){4}s*"
sNumber = " +47 12345678 "
'Check if PhoneNumber match regex pattern
bReturn = oRegEx.Test(sNumber)