Loading ...

Regex to get words ending with "ing"

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Regex to get words ending with "ing"

Regex to get words ending with "ing"

Posts under the topic: Regex to get words ending with "ing"

Posted: 10/19/2010

Lurker 100  points  Lurker
  • Joined on: 9/11/2010
  • Posts: 20

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 ?


tags REGEX

Posted: 10/19/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

#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


tags REGEX, C#
Page 1 of 1 (2 items)