Monday, February 2, 2015

Sub string from a word to another word

Since "cat" can be found inside of other words it is better to use Regex to extract hole words like in the code below



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
string input = "My cat is missing";
string pattern = @"^(\w+\s?)+$";
Regex expr = new Regex(pattern);
Match match = expr.Match(input);
Group group = match.Groups[1];
foreach (Capture capture in group.Captures)
{
Console.WriteLine("Word : {0}", capture.ToString());
}
}
}

}





jdweng


No comments:

Post a Comment