C# - RegEx Return First n Words in a Sentence

By [)ia6l0 iii

Use this snippet.

string regexMatch = string.format(@"([\w]+\s+){"{0}"}", numberOfWords);
string firstNwords = Regex.Match(
inputString,regexMatch).Value;

Related FAQs

Explicity specify the NumberFormatInfo on the parse method of the int class and speed up the Parse Performance.
Use the Environment class to get important and user-specific properties like the User Profile folder.
Named and optional parameters is a new concept introduced in 4.0. The Named Parameters allow you to pass arguments by parameter name instead of parameter position in the signature. Optional Parameters allow parameters to be specified as optional by providing a default value for them in a member declaration. When the member is invoked, optional arguments can be omitted.
Extension method to get the start of a day
Use the default keyword to find if an object is of Reference or Value type
You can truncate the file paths using the Shell LightWeight library.
C# - RegEx Return First n Words in a Sentence  (1238 Views)