"this is word four five six seven eight nine ten eleven twelve thirteen!"
returns first match of..
"this is word four five"
Jeff: Excellent idea!
that works just as well :)
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.
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?
Oh yeah, we definitely should have used "^" so we don't get multiple matches.
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?
What if you had a sentence, and wanted to just return the nth word from a sentence? Like the third or fourth word