Loading ...

Regex to find the string after the word ending with "ing" and before the fullstop (.) | CodeAsp.Net

Regex to find the string after the word ending with "ing" and before the fullstop (.)

Posted in: ASP.NET Topics  »  Getting Started / General ASP.NET
Subscribe to RSS
  • peter-smith

    Hi,

    I have the following string "This is my text string I know. I am loving it I know." I need to find the string after the word ending with "ing" and before the fullstop (.) so in my sample I need the output as "I know" and "it I know". How can I do it ?

     
  •   Answered
    raghav_khunger

     

    #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 I know.";
            string regex = @"(?<=([\w]+((?<=\w)ing\b)\s)).+?(?=\.)";
            var matches = Regex.Matches(input, regex);
            foreach (var match in matches)
            {
                Console.WriteLine(match);
            }
            Console.ReadLine();
            //Output
            //I know
            //it I know
    
        }
    }

     

    <b>[<a href="https://mvp.support.microsoft.com/profile/Raghav">ASP.NET MVP</a>]  [<a href="https://www.microsoftcommunitycontributor.com">Microsoft® Community Contributor</a>] [<a href="http://www.dzone.com/aboutmvb">Dzone MVB</a>] [<a href="https://twitter.com/#!/RaghavKhunger">Follow me on Twitter</a>]</b> <br/><br/><b>[<a href="http://codeasp.net/wiki/view/10/how-to-ask-questions-on-forums">How to ask questions on forums</a>]</b> <br/><br/> "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous <br/> "Success is simply a matter of luck. Ask any failure." Anonymous
     
  • Page 1 of 1 (2 items)