Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Looking for code snippet to parse words from sentence

0 views
Skip to first unread message

Jeff Cochran

unread,
May 29, 2002, 9:13:30 AM5/29/02
to
I have a text file I need to extract specific words from, and I'm
having trouble parsing a line of text into individual words. Is there
an easier way than reading each character until a space and building
the word, then removing that number of characters from the remaining
string and starting again?

Thanks,

Jeff

Helge Wunderlich

unread,
May 29, 2002, 9:23:18 AM5/29/02
to

The "Split" function is exactly what you need.

--
Helge Wunderlich
(Please remove the spam trap if you wish to send email)

Jeff Cochran

unread,
May 29, 2002, 10:14:10 AM5/29/02
to
>>I have a text file I need to extract specific words from, and I'm
>>having trouble parsing a line of text into individual words. Is there
>>an easier way than reading each character until a space and building
>>the word, then removing that number of characters from the remaining
>>string and starting again?
>
>The "Split" function is exactly what you need.

Yup, it was. Why can't I ever seem to find these when I need them...?
:)

Thanks,

Jeff

Michael Harris (MVP)

unread,
May 29, 2002, 12:08:40 PM5/29/02
to
The RegExp solution Steve Fulton gave you in ...inetsdk.programming.scripting.vbscript is a far better solution.

Had you crossposted only one copy question to both NGs at the same time (instead of multiposting separate copies to each) you would have seen Steve's answer here as well...

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US
--


Jeff Cochran

unread,
May 29, 2002, 3:30:24 PM5/29/02
to
>The RegExp solution Steve Fulton gave you in ...inetsdk.programming.scripting.vbscript is a far better solution.

I'm curious why RegExp is a better solution than Split. I don't doubt
you, I have yet to use RegExp for anything.

>Had you crossposted only one copy question to both NGs at the same time (instead of multiposting separate copies to each) you would have seen Steve's answer here as well...

Yeah, yeah. :)

I posted here, then found the other group later. :(

Thanks,

Jeff

Michael Harris (MVP)

unread,
May 29, 2002, 3:56:43 PM5/29/02
to
jcochran at naplesgov dot com (Jeff Cochran) wrote:
>> The RegExp solution Steve Fulton gave you in
>> ...inetsdk.programming.scripting.vbscript is a far better solution.
>
> I'm curious why RegExp is a better solution than Split. I don't doubt
> you, I have yet to use RegExp for anything.
>


Because it handles arbitrary, uneven spacing of words as well as understanding punctuation...

text = "This, is a sentence..."

has only 4 "words".

Using split(text," ") will give more that 4 "words", many that are empty (null strings) and 2 with punctuation tacked on the end as part of the word.

Try this with Steve's RegExp solution and you get only 4 non-empty words with punctuation ignored.

Steve Fulton

unread,
May 29, 2002, 4:20:26 PM5/29/02
to
"Michael Harris (MVP)" <mik...@mvps.org> wrote in message
news:#VmYfr0BCHA.2656@tkmsftngp05...

As long as you're singing my praises, and in light of the OP's innocence of
RegExps, I suppose I should provide a more complete solution. This pattern
will count contractions and hyphenated words as one word:

Sentence = "'This sentence isn't contraction-free,' she said."

With New RegExp
.Pattern = "\b[\w'\-]+\b"
.Global = True
Set Words = .Execute(Sentence)
End With

For Each Word In Words
WScript.Echo Word
Next 'Word

--
When one has too great a dread of what is impending, one feels some relief
when the trouble has come. -Joseph Joubert

=-=-=
Steve
-=-=-


Jeff Cochran

unread,
May 30, 2002, 9:46:47 AM5/30/02
to
Ooohhh, I like... :)

This also solves another issue with Split I've run into, where I don't
know the length of the sentence but need the fifth word and the eighth
word if it exists. Here I just parse each word, if it's what I want I
assign it to a variable, if not, move on.

Thanks again, this is a better lesson than any book. :)

Jeff

0 new messages