List Info

Thread: Parsing a line using RE




Parsing a line using RE
user name
2006-04-18 21:50:13
u'll have to learn how to access Capture Collection members
from Group
Collection: the code below will help; if the regex does not
work in the
code - replace it with yours

//how group collection works:
using System;
using System.Text.RegularExpressions;

public class RegexTest
{
	public static void RunTest()
	{
		int counter;
		Match m;
		CaptureCollection cc;
		GroupCollection gc;
		// declare Regex , Regex pattern
		Regex r = new Regex("(?:\()(?:([\w]+|'[\w\s,\.]+')[,']*)+(?
:\))");
		// Define the string to analyze, find matches
		m = r.Match("var aM =
f.n_P(34621,'User','Click help to continue, or
exit to cancel.',6821,null);");
		//get a group collection
		gc = m.Groups;
		// Print the number of groups.
		Console.WriteLine("Captured groups = " +
gc.Count.ToString());
		// Loop through each group.
		for (int i=0; i < gc.Count; i++)
		{	//get a collection of captures
			cc = gc [ i ].Captures;
			counter = cc.Count;
			// Print number of captures in this group.
			Console.WriteLine("Captures count = " +
counter.ToString());
			// Loop through each capture in group. Print all the
captures
			for (int ii = 0; ii < counter; ii++)
			{
				// Print capture
				Console.WriteLine(cc[ ii ]);
			}
		}
	}

	public static void Main()
	{
		RunTest();
		Console.WriteLine("\nPress Enter to Exit");
		Console.ReadLine();
	}

}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )