wzp
2021-09-16 91912ad24d477ff24be56f0a22438303a8eae552
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<%@ WebHandler Language="C#" Class="GwStrategyTestHandler" %>
 
using System;
using System.Web;
using System.Collections.Generic;
using System.Text.RegularExpressions;
 
public class GwStrategyTestHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string srcContent = ToString(context.Request["srcContent"]);
        string wordList = ToString(context.Request["wordList"]);
 
        IEnumerable<PatternStruct> patternList = ToWordExpressions(wordList);
 
        PatternStruct pattern = null;
 
        string resultMessage = "没有找到匹配内容";
 
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
 
        sw.Start();
 
        if (MatchContent(srcContent, patternList, ref pattern))
        {
            resultMessage = "匹配关键词:" + pattern.Src;
        }
 
        sw.Stop();
 
        resultMessage += ";耗时" + sw.ElapsedTicks / 100 / 1000 + "ms";
 
        context.Response.ContentType = "application/json";
        context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { OK = true, Message = resultMessage }));
    }
 
    public string ToString(string s)
    {
        if (s == null)
            return string.Empty;
 
        return s;
    }
 
    private static bool MatchContent(string srcContent, IEnumerable<PatternStruct> patternList, ref PatternStruct pattern)
    {
        foreach (var p in patternList)
        {
            if (Regex.IsMatch(srcContent, p.Pattern))
            {
                pattern = p;
                return true;
            }
        }
 
        return false;
    }
 
    class PatternStruct
    {
        public string Pattern { get;private set; }
        public string Src { get;private set; }
 
        public PatternStruct(string pattern, string src)
        {
            this.Pattern = pattern;
            this.Src = src;
        }
    }
 
    private IEnumerable<PatternStruct> ToWordExpressions(string source)
    {
        List<PatternStruct> list = new List<PatternStruct>();
 
        string[] lineArray = source.Split(";,;,\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
 
        foreach (string line in lineArray)
        {
            string temp = line;
            temp = temp.Replace(@"\", @"\\");
            temp = temp.Replace(@"|", @"\|");
            temp = temp.Replace(@"^", @"\^");
            temp = temp.Replace(@"$", @"\$");
            temp = temp.Replace(@"{", @"\{");
            temp = temp.Replace(@"}", @"\}");
            temp = temp.Replace(@"(", @"\(");
            temp = temp.Replace(@")", @"\)");
            temp = temp.Replace(@"[", @"\[");
            temp = temp.Replace(@"]", @"\]");
            temp = temp.Replace(@":", @"\:");
            temp = temp.Replace(@".", @"\.");
            temp = temp.Replace(@"?", @"\?");
            temp = temp.Replace(@"-", @"\-");
            temp = temp.Replace(@",", @"\,");
 
            string pattern = Regex.Replace(temp, @"(\*+)", delegate(Match m) { return @"[\w\W]{0," + (m.Groups[1].Value.Length * 10) + "}?"; });
            
            list.Add(new PatternStruct(pattern,line));
        }
 
        return list;
    }
 
 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
 
}