@@ -11,156 +11,60 @@ public static class RegexKeywordsHelper
1111 /// Converts a string list a regexp matching all the given word.
1212 /// </summary>
1313 /// <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>
1614 /// <returns></returns>
17- public static Regex GetRegexFromKeywords ( string [ ] keywords , bool forceAtomicRegex = false , bool noDot = false )
15+ public static Regex GetRegexFromKeywords ( string [ ] keywords )
1816 {
19- if ( forceAtomicRegex )
20- {
21- keywords = ConvertToAtomicRegexAbleStringArray ( keywords ) ;
22- }
23-
2417 if ( keywords . Length == 0 )
2518 {
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
6920 }
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 ) ;
7225 }
7326
74- public static Regex GetRegexFromKeywords ( List < string > keywords , bool ForceAtomicRegex = false )
27+ public static Regex GetRegexFromKeywords ( List < string > keywords )
7528 {
76- if ( ForceAtomicRegex )
77- {
78- keywords = ConvertToAtomicRegexAbleStringArray ( keywords ) ;
79- }
80-
8129 if ( keywords . Count == 0 )
8230 {
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
11732 }
11833
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 ) ;
12237 }
12338
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 )
12544 {
126- var atomicRegexAbleList = new List < string > ( ) ;
127- for ( var j = 0 ; j < keywords . Length ; ++ j )
45+ if ( keywords . Length == 0 )
12846 {
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
13748 }
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 ) ;
15553 }
15654
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 )
15859 {
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 ) ;
16468 }
16569 }
16670}
0 commit comments