Finding whole words in strings

T

Theo van der Ster

Hi,

I often use text files to store reference information such as quotes,
dictionary definitions, synonyms, etc. For searching, I go through all
paragraphs and use Instr to find the word or words I'm looking for.

My question is: Is there a way to confine Instr to looking for whole
words only? I sometimes resort to tricks, such as looking for
space+word+space but then I miss the words at the start and the
beginning of the paragraph, and then there are of course full stops,
commas, etc.

TIA,
Theo
 
J

Jay Freedman

Hi, Theo,

No, there isn't any "whole words" setting for InsStr. I can see a few
alternatives:

- Locate the substring with InStr. Then test the characters before and
after the substring to make sure they're both in the set of delimiters
[space, paragraph mark, list of punctuation]. If not, search again
from the end of the substring.

- Create a temporary document, insert the content of the text file
into it, and use MyRange.Find with .MatchWholeWord = True.

- Learn to use the RegExp object
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsobjregexp.asp)
to do pattern matching.
 
T

Theo van der Ster

Thanks, Jay,

And what a shame. Wouldn't it be easy to have a "whole words" search
capability in Instr. I looked into RegExp but I have to admit it's a
little over my head. I think I'll stick with checking substrings,
unless it slows down my procedures too much.

Thanks again,
Theo

Jay Freedman said:
Hi, Theo,

No, there isn't any "whole words" setting for InsStr. I can see a few
alternatives:

- Locate the substring with InStr. Then test the characters before and
after the substring to make sure they're both in the set of delimiters
[space, paragraph mark, list of punctuation]. If not, search again
from the end of the substring.

- Create a temporary document, insert the content of the text file
into it, and use MyRange.Find with .MatchWholeWord = True.

- Learn to use the RegExp object
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsobjregexp.asp)
to do pattern matching.

Hi,

I often use text files to store reference information such as quotes,
dictionary definitions, synonyms, etc. For searching, I go through all
paragraphs and use Instr to find the word or words I'm looking for.

My question is: Is there a way to confine Instr to looking for whole
words only? I sometimes resort to tricks, such as looking for
space+word+space but then I miss the words at the start and the
beginning of the paragraph, and then there are of course full stops,
commas, etc.

TIA,
Theo
 

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