This repository was archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRegexKeywordsHelper.cs
More file actions
70 lines (61 loc) · 2.37 KB
/
RegexKeywordsHelper.cs
File metadata and controls
70 lines (61 loc) · 2.37 KB
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
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace SPCode.Utils
{
public static class RegexKeywordsHelper
{
/// <summary>
/// Converts a string list a regexp matching all the given word.
/// </summary>
/// <param name="keywords">The words list</param>
/// <returns></returns>
public static Regex GetRegexFromKeywords(string[] keywords)
{
if (keywords.Length == 0)
{
return new Regex("SPEdit_Error"); //We must not return regex that matches any string
}
return new Regex(
@$"\b({string.Join("|", keywords)})\b",
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
}
public static Regex GetRegexFromKeywords(List<string> keywords)
{
if (keywords.Count == 0)
{
return new Regex("SPEdit_Error"); //We must not return regex that matches any string
}
return new Regex(
@$"\b({string.Join("|", keywords.Select(Regex.Escape))})\b",
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
}
/// <summary>
/// Used the match function class like "PrintToChat(...)"
/// </summary>
public static Regex GetFunctionRegex(string[] keywords)
{
if (keywords.Length == 0)
{
return new Regex("SPEdit_Error"); //We must not return regex that matches any string
}
return new Regex(
@$"\b(?<!\.)({string.Join("|", keywords.Select(Regex.Escape))})\b",
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
}
/// <summary>
/// Used the match method class like "myArr.Push"
/// </summary>
public static Regex GetMethodRegex(List<string> keywords)
{
if (keywords.Count == 0)
{
return new Regex("SPEdit_Error"); //We must not return regex that matches any string
}
return new Regex(
@$"\b(?<=[^\s]+\.)({string.Join("|", keywords.Select(Regex.Escape))})\b",
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);
}
}
}