2013-04-24 12:58:19 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
2013-05-03 12:44:51 +00:00
|
|
|
|
namespace Mustache
|
2013-04-24 12:58:19 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides utility methods that require regular expressions.
|
|
|
|
|
/// </summary>
|
2014-05-21 21:02:31 +00:00
|
|
|
|
internal static class RegexHelper
|
2013-04-24 12:58:19 +00:00
|
|
|
|
{
|
2014-05-21 21:02:31 +00:00
|
|
|
|
public const string Key = @"[_\w][_\w\d]*";
|
|
|
|
|
public const string String = @"'.*?'";
|
|
|
|
|
public const string Number = @"[-+]?\d*\.?\d+";
|
|
|
|
|
public const string CompoundKey = "@?" + Key + @"(?:\." + Key + ")*";
|
|
|
|
|
public const string Argument = @"(?:(?<arg_key>" + CompoundKey + @")|(?<arg_string>" + String + @")|(?<arg_number>" + Number + @"))";
|
2013-04-24 12:58:19 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether the given name is a legal identifier.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">The name to check.</param>
|
|
|
|
|
/// <returns>True if the name is a legal identifier; otherwise, false.</returns>
|
|
|
|
|
public static bool IsValidIdentifier(string name)
|
|
|
|
|
{
|
|
|
|
|
if (name == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
Regex regex = new Regex("^" + Key + "$");
|
|
|
|
|
return regex.IsMatch(name);
|
|
|
|
|
}
|
2014-05-21 21:02:31 +00:00
|
|
|
|
|
|
|
|
|
public static bool IsString(string value)
|
|
|
|
|
{
|
|
|
|
|
if (value == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
Regex regex = new Regex("^" + String + "$");
|
|
|
|
|
return regex.IsMatch(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsNumber(string value)
|
|
|
|
|
{
|
|
|
|
|
if (value == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
Regex regex = new Regex("^" + Number + "$");
|
|
|
|
|
return regex.IsMatch(value);
|
|
|
|
|
}
|
2013-04-24 12:58:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|