Untitled
By: a guest | Aug 28th, 2008 | Syntax:
C# | Size: 1.14 KB | Hits: 460 | Expires: Never
using System;
using System.Text.RegularExpressions;
namespace myapp
{
class Class1
{
static void Main(string[] args)
{
String sourcestring = @"[[Text:hi this is a nice sentence[[very nice]] I like this text]] this is not related [[not]]";
Regex re
= new Regex
(@"\[(?>[^\[\]]+|\[(?<DEPTH>)|\](?<-DEPTH>))*(?(DEPTH)(?!))\]");
MatchCollection mc = re.Matches(sourcestring);
// to iterate through the matches and catpure groups
int mIdx = 0;
foreach (Match m in mc)
{
for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
{
Console.WriteLine("[" + mIdx + "][" + re.GetGroupNames()[gIdx] + "] = " + m.Groups[gIdx].Value);
}
mIdx++;
}
// to iterate through the [[Text:... matches only
foreach (Match m in mc)
{
if (m.Groups[0].Value.Substring(0, 7) == "[[Text:")
{
Console.WriteLine(m.Groups[0].Value);
}
}
}
}
}