michaelwarden
asked on
Can't use wildcards in INSTR
I need to search text files for phone numbers. Basically I want to look for (###)###-####, recognize that as a phone number and pick it out of the text file. I try this in INSTR but it doesn't seem to be accepting wildcards (or I'm using them incorrectly). The LIKE operator works but only returns true or false, it doesn't tell me where in the text file it found the match. Any ideas?
I think what he's looking for is a substring like...
"(203)*-1234"
This can be achieved by bracketing the required strings...
first find "(203), then test for "-1234" in the remainder of the primary string.
If the substring was "(203)???-1234" (i.e a fixed number of chars), then rather than testing the 'rest' of the string, test for an exact match at the known position of "(203)" -plus 5 (for (203)) and plus 3 (for the three wildcard) characters.
This could be formed into a funciton fairly easily.
"(203)*-1234"
This can be achieved by bracketing the required strings...
first find "(203), then test for "-1234" in the remainder of the primary string.
If the substring was "(203)???-1234" (i.e a fixed number of chars), then rather than testing the 'rest' of the string, test for an exact match at the known position of "(203)" -plus 5 (for (203)) and plus 3 (for the three wildcard) characters.
This could be formed into a funciton fairly easily.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Use Microsoft VBScript Regular Expressions
Sub FindPhoneNumber(ByVal inputString)
Dim regex, match, matches
Set regex = New RegExp
regex.Pattern = "\(\d{3}\)\d{3}-\d{4}"
regex.Global = True
Set matches = regex.Execute(inputString)
For Each match In matches
Debug.Print match.Value
Next
End Sub
Sub FindPhoneNumber(ByVal inputString)
Dim regex, match, matches
Set regex = New RegExp
regex.Pattern = "\(\d{3}\)\d{3}-\d{4}"
regex.Global = True
Set matches = regex.Execute(inputString)
For Each match In matches
Debug.Print match.Value
Next
End Sub
Where are the references for the RegExp object ?
I've seen them before, but can't remember who/where I saw it used... I also believe that there may be some limitations of RegExp that will thwart michaelwarden's application...
ASKER
i was hoping there was an easier way, but mcoop pointed me in the right direction and i have it working now, thanks a lot for all the posts.
Thanks...
Example:
strPhoneNum = "(607)1156-8896"
strSearch = "15"
iPos = Instr(1,strPhoneNum, strSearch)
iPos will return a number greater than 0 (that's the first position of search string) if the search string find in the whole string.