Wednesday, October 2, 2013

Non-greedy regular expression not working

Is this what you want?



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static string[] inputs = {
"[text1][text2][text3]",
"[text1]",
"[text1] Other Text [text2][text3]",
"[text1][text2]"
};
static void Main(string[] args)
{
string pattern = @"\[(\w+)\]";
Regex expr = new Regex(pattern);
foreach (string input in inputs)
{
int wordCountcount = 1;
MatchCollection matches = expr.Matches(input);
foreach (Match match in matches)
{
Console.Write("Word {0} : {1}; ", wordCountcount, match.Value);
wordCountcount++;
}
Console.WriteLine();
}
}
}
}





jdweng


No comments:

Post a Comment