@@ -11,156 +11,60 @@ public static class RegexKeywordsHelper
11
11
/// Converts a string list a regexp matching all the given word.
12
12
/// </summary>
13
13
/// <param name="keywords">The words list</param>
14
- /// <param name="forceAtomicRegex">If true the word is enclosed in "\b"</param>
15
- /// <param name="noDot">If true the match requires a "." not to be present before the word</param>
16
14
/// <returns></returns>
17
- public static Regex GetRegexFromKeywords ( string [ ] keywords , bool forceAtomicRegex = false , bool noDot = false )
15
+ public static Regex GetRegexFromKeywords ( string [ ] keywords )
18
16
{
19
- if ( forceAtomicRegex )
20
- {
21
- keywords = ConvertToAtomicRegexAbleStringArray ( keywords ) ;
22
- }
23
-
24
17
if ( keywords . Length == 0 )
25
18
{
26
- return new Regex ( "SPEdit_Error" ) ; //hehe
27
- }
28
-
29
- var useAtomicRegex = keywords . All ( t => char . IsLetterOrDigit ( t [ 0 ] ) && char . IsLetterOrDigit ( t [ t . Length - 1 ] ) ) ;
30
-
31
- var regexBuilder = new StringBuilder ( ) ;
32
- regexBuilder . Append ( useAtomicRegex ? @"\b(?>" : @"(" ) ;
33
-
34
- var orderedKeyWords = new List < string > ( keywords ) ;
35
- var i = 0 ;
36
- foreach ( var keyword in orderedKeyWords . OrderByDescending ( w => w . Length ) )
37
- {
38
- if ( i ++ > 0 )
39
- {
40
- regexBuilder . Append ( '|' ) ;
41
- }
42
-
43
- if ( useAtomicRegex )
44
- {
45
- regexBuilder . Append ( Regex . Escape ( keyword ) ) ;
46
- }
47
- else
48
- {
49
- if ( char . IsLetterOrDigit ( keyword [ 0 ] ) )
50
- {
51
- regexBuilder . Append ( @"\b" ) ;
52
- }
53
-
54
- regexBuilder . Append ( Regex . Escape ( keyword ) ) ;
55
- if ( char . IsLetterOrDigit ( keyword [ keyword . Length - 1 ] ) )
56
- {
57
- regexBuilder . Append ( @"\b" ) ;
58
- }
59
- }
60
- }
61
-
62
- if ( useAtomicRegex )
63
- {
64
- regexBuilder . Append ( @")\b" ) ;
65
- }
66
- else
67
- {
68
- regexBuilder . Append ( @")" ) ;
19
+ return new Regex ( "SPEdit_Error" ) ; //We must not return regex that matches any string
69
20
}
70
-
71
- return new Regex ( regexBuilder . ToString ( ) , RegexOptions . CultureInvariant | RegexOptions . ExplicitCapture ) ;
21
+
22
+ return new Regex (
23
+ @$ "\b({ string . Join ( "|" , keywords ) } )\b",
24
+ RegexOptions . CultureInvariant | RegexOptions . ExplicitCapture ) ;
72
25
}
73
26
74
- public static Regex GetRegexFromKeywords ( List < string > keywords , bool ForceAtomicRegex = false )
27
+ public static Regex GetRegexFromKeywords ( List < string > keywords )
75
28
{
76
- if ( ForceAtomicRegex )
77
- {
78
- keywords = ConvertToAtomicRegexAbleStringArray ( keywords ) ;
79
- }
80
-
81
29
if ( keywords . Count == 0 )
82
30
{
83
- return new Regex ( "SPEdit_Error" ) ; //hehe
84
- }
85
-
86
- var useAtomicRegex = keywords . All ( t => char . IsLetterOrDigit ( t [ 0 ] ) && char . IsLetterOrDigit ( t [ t . Length - 1 ] ) ) ;
87
-
88
- var regexBuilder = new StringBuilder ( ) ;
89
- regexBuilder . Append ( useAtomicRegex ? @"\b(?>" : @"(" ) ;
90
-
91
- var orderedKeyWords = new List < string > ( keywords ) ;
92
- var i = 0 ;
93
- foreach ( var keyword in orderedKeyWords . OrderByDescending ( w => w . Length ) )
94
- {
95
- if ( i ++ > 0 )
96
- {
97
- regexBuilder . Append ( '|' ) ;
98
- }
99
-
100
- if ( useAtomicRegex )
101
- {
102
- regexBuilder . Append ( Regex . Escape ( keyword ) ) ;
103
- }
104
- else
105
- {
106
- if ( char . IsLetterOrDigit ( keyword [ 0 ] ) )
107
- {
108
- regexBuilder . Append ( @"\b" ) ;
109
- }
110
-
111
- regexBuilder . Append ( Regex . Escape ( keyword ) ) ;
112
- if ( char . IsLetterOrDigit ( keyword [ keyword . Length - 1 ] ) )
113
- {
114
- regexBuilder . Append ( @"\b" ) ;
115
- }
116
- }
31
+ return new Regex ( "SPEdit_Error" ) ; //We must not return regex that matches any string
117
32
}
118
33
119
- regexBuilder . Append ( useAtomicRegex ? @")\b" : @")" ) ;
120
-
121
- return new Regex ( regexBuilder . ToString ( ) , RegexOptions . CultureInvariant | RegexOptions . ExplicitCapture ) ;
34
+ return new Regex (
35
+ @$ "\b( { string . Join ( "|" , keywords ) } )\b" ,
36
+ RegexOptions . CultureInvariant | RegexOptions . ExplicitCapture ) ;
122
37
}
123
38
124
- public static string [ ] ConvertToAtomicRegexAbleStringArray ( string [ ] keywords )
39
+
40
+ /// <summary>
41
+ /// Used the match function class like "PrintToChat(...)"
42
+ /// </summary>
43
+ public static Regex GetFunctionRegex ( string [ ] keywords )
125
44
{
126
- var atomicRegexAbleList = new List < string > ( ) ;
127
- for ( var j = 0 ; j < keywords . Length ; ++ j )
45
+ if ( keywords . Length == 0 )
128
46
{
129
- if ( keywords [ j ] . Length > 0 )
130
- {
131
- if ( char . IsLetterOrDigit ( keywords [ j ] [ 0 ] ) &&
132
- char . IsLetterOrDigit ( keywords [ j ] [ keywords [ j ] . Length - 1 ] ) )
133
- {
134
- atomicRegexAbleList . Add ( keywords [ j ] ) ;
135
- }
136
- }
47
+ return new Regex ( "SPEdit_Error" ) ; //We must not return regex that matches any string
137
48
}
138
-
139
- return atomicRegexAbleList . ToArray ( ) ;
140
- }
141
-
142
- private static List < string > ConvertToAtomicRegexAbleStringArray ( IEnumerable < string > keywords )
143
- {
144
- return keywords . Where ( t => t . Length > 0 )
145
- . Where ( t => char . IsLetterOrDigit ( t [ 0 ] ) && char . IsLetterOrDigit ( t [ t . Length - 1 ] ) ) . ToList ( ) ;
146
- }
147
-
148
- public static Regex GetRegexFromKeywords2 ( string [ ] keywords )
149
- {
150
- var regexBuilder = new StringBuilder ( @"\b(?<=[^\s]+\.)(" ) ;
151
- regexBuilder . Append ( string . Join ( "|" , keywords ) ) ;
152
- regexBuilder . Append ( @")\b" ) ;
153
-
154
- return new Regex ( regexBuilder . ToString ( ) , RegexOptions . CultureInvariant | RegexOptions . ExplicitCapture ) ;
49
+
50
+ return new Regex (
51
+ @$ "\b(?<!\.)({ string . Join ( "|" , keywords ) } )\b",
52
+ RegexOptions . CultureInvariant | RegexOptions . ExplicitCapture ) ;
155
53
}
156
54
157
- public static Regex GetRegexFromKeywords2 ( List < string > keywords )
55
+ /// <summary>
56
+ /// Used the match method class like "myArr.Push"
57
+ /// </summary>
58
+ public static Regex GetMethodRegex ( List < string > keywords )
158
59
{
159
- var regexBuilder = new StringBuilder ( @"\b(?<=[^\s]+\.)(" ) ;
160
- regexBuilder . Append ( string . Join ( "|" , keywords ) ) ;
161
- regexBuilder . Append ( @")\b" ) ;
162
-
163
- return new Regex ( regexBuilder . ToString ( ) , RegexOptions . CultureInvariant | RegexOptions . ExplicitCapture ) ;
60
+ if ( keywords . Count == 0 )
61
+ {
62
+ return new Regex ( "SPEdit_Error" ) ; //We must not return regex that matches any string
63
+ }
64
+
65
+ return new Regex (
66
+ @$ "\b(?<=[^\s]+\.)({ string . Join ( "|" , keywords ) } )\b",
67
+ RegexOptions . CultureInvariant | RegexOptions . ExplicitCapture ) ;
164
68
}
165
69
}
166
70
}
0 commit comments