1
wangsheng
2022-03-22 49d5052a4a51a639d1c75d6feca56f8ae2e5e568
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
namespace com.softwee.smgw.common
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Text.RegularExpressions;
 
    public abstract class RegexHelper
    {
        protected RegexHelper()
        {
        }
 
        public static List<Regex> ToRegExpressions(string source)
        {
            List<Regex> list = new List<Regex>();
            Regex regex = new Regex(@"(\(.*?\|.*?\)|\*+)|^(\(.*?\|.*?\)|\*+)", RegexOptions.IgnoreCase);
            Regex regex2 = new Regex(@"[\[\]\}\{\.\-\,\^]", RegexOptions.IgnoreCase);
            foreach (string str in source.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                try
                {
                    string input = regex2.Replace(str, m => @"\" + m.Value);
                    string str3 = regex.Replace(input, delegate(Match m)
                    {
                        if (!m.Value.StartsWith("(") && m.Value.StartsWith("*"))
                        {
                            return @"[\w\W]{0," + (m.Value.Length * 10) + "}?";
                        }
                        return m.Value;
                    });
                    list.Add(new Regex(str3 + "$"));
                }
                catch (Exception)
                {
                }
            }
            return list;
        }
 
        public static bool ValidateRegex(string source, out string errorString)
        {
            List<Regex> list = new List<Regex>();
            errorString = string.Empty;
            Regex regex = new Regex(@"(\(.*?\|.*?\)|\*+)|^(\(.*?\|.*?\)|\*+)", RegexOptions.IgnoreCase);
            Regex regex2 = new Regex(@"[\[\]\}\{\.\-\,\?\+\^]", RegexOptions.IgnoreCase);
            foreach (string str in source.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                try
                {
                    string input = regex2.Replace(str, m => @"\" + m.Value);
                    string pattern = regex.Replace(input, delegate(Match m)
                    {
                        if (!m.Value.StartsWith("(") && m.Value.StartsWith("*"))
                        {
                            return @"[\w\W]{0," + (m.Value.Length * 10) + "}?";
                        }
                        return m.Value;
                    });
                    list.Add(new Regex(pattern));
                }
                catch (Exception exception)
                {
                    errorString = errorString + exception.Message + "\n";
                }
            }
            if (errorString.Length > 100)
            {
                errorString = errorString.Substring(0, 100) + "……";
            }
            return string.IsNullOrEmpty(errorString);
        }
    }
}