Solved

Can't use wildcards in INSTR

Posted on 2002-07-28
7
2,157 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 50

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

792 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