Posted: 10/19/2010
Hi,I have the following string "This is my text string. I am loving it." I need to extract thos words which are ending with "ing" so in my sample I need the output as "string" and "loving" . 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 am loving it."; string regex = @"[\w]+((?<=\w)ing\b)"; var matches = Regex.Matches(input, regex); foreach (var match in matches) { Console.WriteLine(match); } Console.ReadLine(); //Output: string // loving } }
Explanation of regex:
[\w]+((?<=\w)ing\b)
Match a single character that is a “word character” (letters, digits, etc.) «[\w]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regular expression below and capture its match into backreference number 1 «((?<=\w)ing\b)»
Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=\w)»
Match a single character that is a “word character” (letters, digits, etc.) «\w»
Match the characters “ing” literally «ing»
Assert position at a word boundary «\b»
Created with RegexBuddy