The file you're working with appears to be fixed width; whoever wrote the program that generates this file, he was communicating the meaning of each field by its position. So it is best that your program consume the information the way it was passed, by interpreting the data based on its position, not its ability to match a particular regular expression. That being said, regular expressions would be a great way to validate the data after it is parsed.
To work with this kind of data, I would probably build a class that represents a single record, and give it methods for parsing and validating. Here is something I came up with pretty quickly:
public class DetailRecord
{
private readonly string _originalText;
static private Dictionary<string, Func<string,string>> _map = new Dictionary<string, Func<string,string>>
{
{ "ContractNo", s => s.Substring( 1 ,10 ) },
{ "BankNum", s => s.Substring( 15 , 8 ) },
{ "ShortName", s => s.Substring( 35 ,10 ).Trim() }
};
public DetailRecord(string originalText)
{
_originalText = originalText;
}
public string this[string key]
{
get
{
return _map[key](_originalText);
}
}
public string BankNum
{
get { return this["BankNum"]; }
}
public string ContractNo
{
get { return this["ContractNo"]; }
}
public string ShortName
{
get { return this["ShortName"]; }
}
public bool IsValid
{
get
{
int dummy;
if (!int.TryParse(this.ContractNo, out dummy)) return false;
if (!Regex.IsMatch(this.BankNum, @"[A-Z]\d\d\s\s\d\d\d")) return false;
return true;
}
}
}
You'll notice this class keeps a static dictionary (the _map) which contains a list of functions for parsing each field.
Also notice there is an IsValid property which uses a regular expression to validate the bank number. The contract number appears to be straight numeric, and it validates that too.
Test program:
public class Program
{
public static void Main()
{
var input = " 0759651386 X08 606 0209784104 BURTON 3334.24";
var line = new DetailRecord(input);
if (line.IsValid)
{
Console.WriteLine("Contract number: '{0}'", line.ContractNo);
Console.WriteLine("Bank number: '{0}'", line.BankNum);
Console.WriteLine("Short name: '{0}'", line.ShortName);
}
}
}
Output:
Contract number: '0759651386'
Bank number: 'X08 606'
Short name: 'BURTON'
See my code on DotNetFiddle
Pattern1 = "(?<![a-z])[0-9]{9}"(?:[a-z][0-9]{6}|[0-9]{9})contains a list of alternations where the most important is listed first. It's equivalent to(?:abcd|abc|ab|a)sort of.. but you get the idea.