Solved

Can't use wildcards in INSTR

Posted on 2002-07-28
7
2,103 Views
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?
0
Comment
Question by:michaelwarden
7 Comments
 
LVL 49

Expert Comment

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

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.
0
 
LVL 4

Expert Comment

by:mcoop
ID: 7184539
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.
0
 
LVL 4

Accepted Solution

by:
mcoop earned 75 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)

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:TigerZhao
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
    Next
End Sub
0
 
LVL 4

Expert Comment

by:mcoop
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...
0
 

Author Comment

by:michaelwarden
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.
0
 
LVL 4

Expert Comment

by:mcoop
ID: 7186575
Thanks...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now