using System.Text.RegularExpressions;
namespace HStation.Service
{
///
/// 正则辅助类
///
internal static class RevitJsonRegexHelper
{
private const string _numericPattern = @"[+-]?\d*[.]?\d*";//数值模式
private const string _numericAbsolutePattern = @"^[+-]?\d*[.]?\d*$";//数值绝对模式
///
/// 匹配第一个数值
///
public static bool MatchNumeric(this string numericStr, out double numeric)
{
numeric = 0;
if (string.IsNullOrEmpty(numericStr))
{
return false;
}
var match = Regex.Match(numericStr, _numericPattern);
if (string.IsNullOrEmpty(match.Value))
{
return false;
}
if (Regex.IsMatch(match.Value, _numericAbsolutePattern))
{
numeric = double.Parse(match.Value);
return true;
}
return false;
}
///
/// 匹配第一个数值
///
public static bool MatchNumeric(this JToken jtoken, out double numeric)
{
numeric = 0;
if (jtoken == null)
{
return false;
}
return jtoken.ToString().MatchNumeric(out numeric);
}
}
}