public static string Format(string template, object item, IFormatProvider provider = null)
{
var result = _templateRegex.Replace(template, m =>
{
if (IsBalancedDelimiterCountEven(m))
{
return FormatEscapedToken(m.Value);
}
if (IsBalancedDelimiterCountOdd(m))
{
string property = GetMatchPropertyValue(item, m, provider);
return FormatOddBalancedToken(m, property);
}
if (IsPartiallyDelimited(m))
{
Func<string> propertyValue = () => GetMatchPropertyValue(item, m, provider);
return FormatPartiallyDelimitedToken(m, propertyValue);
}
return m.Value;
});
return result;
}