using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Text.RegularExpressions;
public static void Main()
string FormatExpression = @"£###,###,##0.00 a year";
var regex = new Regex("^([^#^0])+");
Match match = regex.Match(FormatExpression);
string prefix = match.Value;
regex = new Regex("[^#^0]+$");
match = regex.Match(FormatExpression);
string suffix = match.Value;
var numString = FormatExpression.Substring(prefix.Length, FormatExpression.Length - prefix.Length - suffix.Length);
var seperators = new Dictionary<string, int>();
regex = new Regex("[^#^0]");
foreach(Match m in regex.Matches(numString)) {
if (!seperators.ContainsKey(m.Value)) {
seperators.Add(m.Value, 1);
char? thousandseperator = null;
char? decimalseperator = null;
switch (seperators.Count) {
if (seperators.First().Value==1) {
decimalseperator = seperators.First().Key[0];
thousandseperator = seperators.First().Key[0];
if (seperators.Last().Value>1) throw new Exception("Invalid format expression");
thousandseperator = seperators.First().Key[0];
decimalseperator = seperators.Last().Key[0];
throw new Exception("Invalid format expression");
bool forceTrailingZeroes = false;
if (decimalseperator!=null) {
regex = new Regex("[^" + decimalseperator + "]+$");
match = regex.Match(numString);
numDecimals = match.Value.Length;
regex = new Regex("[^0]");
forceTrailingZeroes = !regex.IsMatch(match.Value);
Console.WriteLine("prefix: " + prefix + " length: " + prefix.Length);
Console.WriteLine("suffix: " + suffix + " length: " + suffix.Length);
Console.WriteLine("numString: " + numString + " length: " + numString.Length);
if (decimalseperator!=null) Console.WriteLine("decimalseperator: " + decimalseperator);
if (thousandseperator!=null) Console.WriteLine("thousandseperator: " + thousandseperator);
Console.WriteLine("num decimals: " + numDecimals.ToString());
Console.WriteLine("forceTrailingZeroes: " + forceTrailingZeroes.ToString());