-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularExpressionSearch.cs
More file actions
42 lines (37 loc) · 1.28 KB
/
RegularExpressionSearch.cs
File metadata and controls
42 lines (37 loc) · 1.28 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
namespace DA.Algorithms.Strings
{
public static class RegularExpressionSearch
{
/// <summary>
/// "?" - matches any single character.
/// "*" - matches zero or more of the preceding element.
/// </summary>
public static bool MatchExpression (char[] expression, char[] text)
{
return MatchExpression (expression, text, 0, 0);
}
private static bool MatchExpression (char[] expression, char[] text, int i, int j)
{
if (i == expression.Length && j == text.Length)
{
return true;
}
if ((i == expression.Length && j != text.Length)
|| (i != expression.Length && j == text.Length))
{
return false;
}
if (expression[i] == '?' || expression[i] == expression[j])
{
return MatchExpression (expression, text, i + 1, j + 1);
}
if (expression[i] == '*')
{
return MatchExpression (expression, text, i + 1, j)
|| MatchExpression (expression, text, i, j + 1)
|| MatchExpression (expression, text, i + 1, j + 1);
}
return false;
}
}
}