Good day everyone
I was wondering if it's possible to define multiple patterns to one RegEx.
I need to extract a phone number from a TextBox string in a UserForm but the number could have different patterns like
.Pattern = "([0-9]{2} [0-9]{4} [0-9]{6})"
or
.Pattern = "([0-9]{3}-[0-9]{3}-[0-9]{4})"
and so on...
The text string itself is composed of:
A client name, an address, an email, a price a PHONE NUMBER (not always formatted the same way, thus the reason i need multiple patterns) and other text, and not always in this order.
This is just part of the script i'm using which works fine for ONE pattern
Thanx in advance.
I was wondering if it's possible to define multiple patterns to one RegEx.
I need to extract a phone number from a TextBox string in a UserForm but the number could have different patterns like
.Pattern = "([0-9]{2} [0-9]{4} [0-9]{6})"
or
.Pattern = "([0-9]{3}-[0-9]{3}-[0-9]{4})"
and so on...
The text string itself is composed of:
A client name, an address, an email, a price a PHONE NUMBER (not always formatted the same way, thus the reason i need multiple patterns) and other text, and not always in this order.
This is just part of the script i'm using which works fine for ONE pattern
VBA Code:
If Not IsNull(sInput) Then
Set oRegEx = CreateObject("VBScript.RegExp")
With oRegEx
.Pattern = "([0-9]{2} [0-9]{4} [0-9]{6})" ' Tried using "([0-9]{2} [0-9]{4} [0-9]{6})" or "([0-9]{3}-[0-9]{3}-[0-9]{4})" but obviously didn't work.
.Global = True
.IgnoreCase = True
.MultiLine = True
Set oMatches = .Execute(sInput)
End With
For Each oMatch In oMatches
sPhoneNo = oMatch.value & "," & sPhoneNo
Next oMatch
If Right(sPhoneNo, 1) = "," Then sPhoneNo = Left(sPhoneNo, Len(sPhoneNo) - 1)
ExtractPhoneNo = Split(sPhoneNo, ",") 'Return an array of phone numbers extracted from sInput
Else
ExtractPhoneNo = Null
End If
Thanx in advance.