Wildcard search on memory string

D

DA

I'm looking for suggestions on how to do a wildcard
search on a string held in memory.

Case:
varIn - is holding a string read from an external file.
(There's no open document)
term - is the search item I want to find in my varIn

Using:
If instr(varIn,term)<> 0 Then
'I've got a hit
Endif

Problem:
1)I do NOT want to return a hit if the term is found
inside a word (e.g. term = "BLE" varIn = "babble")

2)I DO want to return a hit if the term is found by
itself, at the end of a sentence, or in combination with
punctuation. (e.g. term = "BLE" varIn = "BLEs are useful
in combination with a BLE-generator." - should return
hits on both items.)

Building a long list of case statements is my last
resort, which I'm trying to avoid here.

I was hoping that I could do something with wildcards
(e.g. instr(varIn, term & [ ,s-_'.,])

Any ideas on a solution?
Dennis
 
J

Jonathan West

Hi Dennis,

The "Like" operator is designed for just this sort of use.

something like this should do the needful

If varIn Like "[!a-zA-Z]" & term & "[!a-zA-Z]" Then
'you have a hit
End If
 
D

DA

Jonathan,

Thanks for your reply. After my post I had some success
by using the RegExp object (calling VBScript API).

Since my input string can be really complex and long, I
didn't think that "Like" would work.

As per your suggestion, I am now using the following,
which works a treat! - Thanks again.

If varIn Like ("*" & "[!a-zA-Z]" & term & _
"[ s_,.;':-]" & "*") Then
MsgBox "Yippee!"
End If

One last question... the "-" needs to be covered in the
allowable character list. Putting the "-" last in the
list seems to work OK, but is this just coincidence? Does
the minus always get treated as a range character?


-----Original Message-----
Hi Dennis,

The "Like" operator is designed for just this sort of use.

something like this should do the needful

If varIn Like "[!a-zA-Z]" & term & "[!a-zA-Z]" Then
'you have a hit
End If

--
Regards
Jonathan West - Word MVP
http://www.multilinker.com
Please reply to the newsgroup


DA said:
I'm looking for suggestions on how to do a wildcard
search on a string held in memory.

Case:
varIn - is holding a string read from an external file.
(There's no open document)
term - is the search item I want to find in my varIn

Using:
If instr(varIn,term)<> 0 Then
'I've got a hit
Endif

Problem:
1)I do NOT want to return a hit if the term is found
inside a word (e.g. term = "BLE" varIn = "babble")

2)I DO want to return a hit if the term is found by
itself, at the end of a sentence, or in combination with
punctuation. (e.g. term = "BLE" varIn = "BLEs are useful
in combination with a BLE-generator." - should return
hits on both items.)

Building a long list of case statements is my last
resort, which I'm trying to avoid here.

I was hoping that I could do something with wildcards
(e.g. instr(varIn, term & [ ,s-_'.,])

Any ideas on a solution?
Dennis

.
 
J

Jonathan West

DA said:
Jonathan,

Thanks for your reply. After my post I had some success
by using the RegExp object (calling VBScript API).

Since my input string can be really complex and long, I
didn't think that "Like" would work.

As per your suggestion, I am now using the following,
which works a treat! - Thanks again.

If varIn Like ("*" & "[!a-zA-Z]" & term & _
"[ s_,.;':-]" & "*") Then
MsgBox "Yippee!"
End If

One last question... the "-" needs to be covered in the
allowable character list. Putting the "-" last in the
list seems to work OK, but is this just coincidence? Does
the minus always get treated as a range character?

According to the VBA help on the Like operator "A hyphen (-) can appear
either at the beginning (after an exclamation point if one is used) or at
the end of charlist to match itself. In any other location, the hyphen is
used to identify a range of characters."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top