MustacheSharp/mustache-sharp/RegexHelper.cs

53 lines
1.6 KiB
C#
Raw Normal View History

using System;
using System.Text.RegularExpressions;
namespace Mustache
{
/// <summary>
/// Provides utility methods that require regular expressions.
/// </summary>
2014-05-21 21:02:31 +00:00
internal static class RegexHelper
{
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 + @"))";
/// <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);
}
}
}