lixiaojun
2024-08-14 eb4679bfe7f2c945f3e03f6927c8fde893f7d33c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Text.RegularExpressions;
 
namespace HStation.Service
{
    /// <summary>
    /// 正则辅助类
    /// </summary>
    internal static class RevitJsonRegexHelper
    {
        private const string _numericPattern = @"[+-]?\d*[.]?\d*";//数值模式
        private const string _numericAbsolutePattern = @"^[+-]?\d*[.]?\d*$";//数值绝对模式
 
        /// <summary>
        /// 匹配第一个数值
        /// </summary>
        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;
        }
 
        /// <summary>
        /// 匹配第一个数值
        /// </summary>
        public static bool MatchNumeric(this JToken jtoken, out double numeric)
        {
            numeric = 0;
            if (jtoken == null)
            {
                return false;
            }
            return jtoken.ToString().MatchNumeric(out numeric);
        }
 
 
    }
}