Page 1 of 1

RegularExpression-find first word/spaces

Posted: Sun Oct 25, 2009 11:27 pm
by WilliamL
..been reading about RegularExpressions.

..still lost.

So how would I find just the first word in a sentence?
e.g. Sylvester is black. (Sylvester only)
e.g. Sylvester-cat is black. (Sylvester only)

All the blanks out of the sentence above?

Find a simpler tutorial than http://www.regular-expressions.info/ ?

Re: RegularExpression-find first word/spaces

Posted: Mon Oct 26, 2009 1:34 am
by Kale

Code: Select all

Dim Matches.s(0)

CreateRegularExpression(0, "^[\w]+")

ExtractRegularExpression(0, "Sylvester is black.", Matches())
Debug Matches(0)

ExtractRegularExpression(0, "Sylvester-cat is black.", Matches())
Debug Matches(0)
http://www.codinghorror.com/blog/archives/001016.html

Re: RegularExpression-find first word/spaces

Posted: Mon Oct 26, 2009 3:00 am
by WilliamL
Thanks Kale,

Lets see... "^[\w]+"
^ is start at beginning of string
[\w] is a special code for 'look for any alfa char'
+ is repeat (appears to repeat until there aren't any more alfa chars)

I'm studying your code and will be spending some time reading your suggested link.

Such a simple concept... :?

[later]
I ordered a book, 'Mastering Regular Expressions', from Amazon. That should answer all my questions.
Ya' know they're right here in town!

Re: RegularExpression-find first word/spaces

Posted: Mon Oct 26, 2009 7:59 am
by helpy
Quote from http://www.pcre.org/pcre.txt:[quote] \d any decimal digit
\D any character that is not a decimal digit
\h any horizontal whitespace character
\H any character that is not a horizontal whitespace character
\s any whitespace character
\S any character that is not a whitespace character
\v any vertical whitespace character
\V any character that is not a vertical whitespace character
\w any "word" character
\W any "non-word" character[/quote]

Re: RegularExpression-find first word/spaces

Posted: Mon Oct 26, 2009 9:20 am
by Fred
Ho, helpy is back ! :)

Re: RegularExpression-find first word/spaces

Posted: Mon Oct 26, 2009 7:27 pm
by Kale
WilliamL wrote: + is repeat (appears to repeat until there aren't any more alfa chars)
To be more precise, + means 1 or more (unlimited), where'as if i had used * it would mean 0 or more.

Regular expression aren't hard per'say, they're just very easy to get lost in.