Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Can't use wildcards in INSTR

Posted on 2002-07-28
Medium Priority
Last Modified: 2010-05-18
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?
Question by:michaelwarden
LVL 55

Expert Comment

by:Ryan Chong
ID: 7184521
Actually INSTR function will perform as wildcard LIKE:


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.

Expert Comment

ID: 7184539
I think what he's looking for is a substring like...

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.

Accepted Solution

mcoop earned 300 total points
ID: 7184578
Oh - and to refer to the actual example  (###)###-####

I would probably look in a slightly different way....

Searh for the ( and ) first (least likely to occur), then the block of 4 numerics, then walk backwards to check the area code and exchange prefix digits

Of course you also should consider the fact that some people don't put the punctuation where it should be (capitalisation, hyphens and spaces etc)  so perhaps it is worth pre-processing and index that file against the source file  to remove all ambiguities, only then scan for the formatted number strings, and only then refer back to the original file to extract the entry as actually keyed in... (if that's really necessary)

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Expert Comment

ID: 7184812
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
End Sub

Expert Comment

ID: 7185141

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...

Author Comment

ID: 7186217
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.

Expert Comment

ID: 7186575

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question