sometimes we need to do weird things! lets say you have a situation that you need to parse through some big text and to extract some specific fields.

okay lets RegEx get into work.

Download�RegExTester (i bet you can do that in a blink of an eye). This will generate the C# code snippet for you. Consider below two text format.

Target_String1 = “$06022014|1|W|10022014|Fiona Mactaggart|Slough|To ask the Secretary of etc etc. 187084”;

Target_String2 = “$27012014|1|W|10022014|Baroness Kinnock of Holyhead||To ask Her Majesty’s Government what bla bla bla. HL4988”;

Below two is the RegEx for the two sentences. Need to identify the separator, even the space is important sometimes.

[1]	(\$)(\d+)*\|*(\d+) *\| *(w|o|n)*\|*(\d+)*\|*(.+) *\| *\|*(.+) *\| *(to ask.*) *\. *(.+)
[2]	(\d+)*\|*(\d+)*\|*(w|o|n) *\| *(\d+) *\| *(.+) *\| *\| *(.+)*\. *(.+)

RegExTester will now generate the C# snippets.

Regex regex = new Regex(@"(\$)(\d+)*\|*(\d+) *\| *(w|o|n)*\|*(\d+)*\|*(.+) *\| *\|*(.+) *\| *(to ask.*) *\. *(.+)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline | RegexOptions.Singleline);
MatchCollection matchCollection = regex.Matches( [Target_String1] );
foreach (Match match in matchCollection)
{
//do some work;
DateTime DateTabled = DateTime.ParseExact(aMatch.Groups[1].Value, "ddMMyyyy", System.Globalization.CultureInfo.CurrentCulture);
string Name = aMatch.Groups[5].Value;
}
Regex regex = new Regex(@"(\d+)*\|*(\d+)*\|*(w|o|n) *\| *(\d+) *\| *(.+) *\| *\| *(.+)*\. *(.+)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline | RegexOptions.Singleline);
MatchCollection matchCollection = regex.Matches( [Target_String2] );
foreach (Match match in matchCollection)
{
//do some work;
//Same way the last method works
}