Using Regex to return the first N words in a string

My blog has moved. You can view this post at the following address: http://www.osherove.com/blog/2005/1/7/using-regex-to-return-the-first-n-words-in-a-string.html
Published Friday, January 07, 2005 4:47 AM by RoyOsherove

Comments

Thursday, January 06, 2005 10:40 PM by patag

# re: Using Regex to return the first N words in a string

Why not just look for the Nth single space?
Thursday, January 06, 2005 11:52 PM by Jeff Atwood

# re: Using Regex to return the first N words in a string

Why do you need the stringbuilder? Just return the first match from..

Regex.Match(s, "(\w+\s+){5}").ToString().Trim

given input of...

"this is word four five six seven eight nine ten eleven twelve thirteen!"

returns first match of..

"this is word four five"
Friday, January 07, 2005 6:18 AM by TrackBack

# ...Oh, and I'm also

Friday, January 07, 2005 7:51 AM by Roy Osherove

# re: Using Regex to return the first N words in a string

Jeff: Excellent idea!
that works just as well :)
Friday, January 07, 2005 9:24 AM by Aaron Robinson

# re: Using Regex to return the first N words in a string

And if Jeff's version doesn't have any matches, you'd just spit out the original string, on the assumption that it didn't have at least N words.
Friday, January 07, 2005 9:25 AM by Sam Smoot

# re: Using Regex to return the first N words in a string

If you replace spaces with word boundries then you don't have to Trim(), and punctuation won't break the function:

^(\w+\b.*?){4}

Also, this matches only the first set, so theoretically it may be faster?
Saturday, January 08, 2005 2:09 AM by Jeff Atwood

# re: Using Regex to return the first N words in a string

Oh yeah, we definitely should have used "^" so we don't get multiple matches.
Sunday, January 09, 2005 4:39 AM by Alex Lvovich

# re: Using Regex to return the first N words in a string

I have a function that checks if string contains a specific pattern, I do it by using string.IndexOf method. Using regex seem to be more elegant solution for this. My question is if using regex is more efficient way for this problem?
Friday, January 14, 2005 9:12 AM by Green Dragon

# re: Using Regex to return the first N words in a string

Hey Roy, hope you don't mind that i linked to your post in my Wiki: http://greendragon.myserver.org:1600/FlexWiki/default.aspx/InfiniteLoops.FindFirstNWordsInString
Wednesday, August 09, 2006 12:47 PM by David

# re: Using Regex to return the first N words in a string

What if you had a sentence, and wanted to just return the nth word from a sentence? Like the third or fourth word