Posted: 10/19/2010
Hi,I have the following string "This is my text string I know. I am loving it." I need to extract the previous word before those words which are ending with "ing" so in my sample I need the output as "text" and "am". How can I do it ?
#region Using Directives using System; using System.Text.RegularExpressions; #endregion public class Example { static void Main() { string input = "This is my text string I know. I am loving it."; string regex = @"(\w+)(?=((\s)(?=([\w]+((?<=\w)ing\b)))))"; var matches = Regex.Matches(input, regex); foreach (var match in matches) { Console.WriteLine(match); } Console.ReadLine(); //Output: text // am } }