-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlternation.cs
More file actions
68 lines (63 loc) · 3.3 KB
/
Alternation.cs
File metadata and controls
68 lines (63 loc) · 3.3 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
using System;
namespace ReadableRex.Patterns
{
/// <summary>
/// Provides methods to create alternation patterns in regular expressions.
/// </summary>
/// <remarks>The <see cref="Alternation"/> class allows the construction of complex regular expression
/// patterns that include conditional and either/or logic. It is designed to work with a preceding pattern and
/// extends its capabilities by adding alternation constructs.</remarks>
public class Alternation
{
readonly Pattern _precedingPattern;
internal Alternation(Pattern precedingPattern)
{
_precedingPattern = precedingPattern;
}
/// <summary>
/// Creates a pattern that matches either the first option or the second option.
/// </summary>
/// <param name="firstOption"></param>
/// <param name="secondOption"></param>
/// <returns></returns>
public Pattern Either(Pattern firstOption, Pattern secondOption)
{
return _precedingPattern.RegEx( String.Format("({0}|{1})", firstOption, secondOption) );
}
/// <summary>
/// Constructs a conditional pattern that applies one of two patterns based on a match condition.
/// </summary>
/// <param name="matched">The pattern to test for a match. If this pattern matches, the <paramref name="then"/> pattern is applied.</param>
/// <param name="then">The pattern to apply if the <paramref name="matched"/> pattern is successful.</param>
/// <param name="otherwise">The pattern to apply if the <paramref name="matched"/> pattern is not successful.</param>
/// <returns>A new <see cref="Pattern"/> that represents the conditional application of the <paramref name="then"/> or
/// <paramref name="otherwise"/> pattern based on the <paramref name="matched"/> pattern.</returns>
public Pattern IfPattern(Pattern matched, Pattern then, Pattern otherwise)
{
return _precedingPattern.RegEx(String.Format("(?(?={0}){1}|{2})", matched, then, otherwise));
}
/// <summary>
/// Creates a conditional pattern that applies one of two specified patterns based on the presence of a named
/// group.
/// </summary>
/// <param name="namedGroupToMatch">The name of the group to check for a match.</param>
/// <param name="then">The pattern to apply if the named group is matched.</param>
/// <param name="otherwise">The pattern to apply if the named group is not matched.</param>
/// <returns>A <see cref="Pattern"/> that represents the conditional application of the specified patterns.</returns>
public Pattern IfNamedGroup(string groupName, Pattern then, Pattern otherwise)
{
return _precedingPattern.RegEx(String.Format("(?({0}){1}|{2})", groupName, then, otherwise));
}
/// <summary>
///
/// </summary>
/// <param name="unnamedCaptureToMatch"></param>
/// <param name="then"></param>
/// <param name="otherwise"></param>
/// <returns></returns>
public Pattern IfUnnamedCapture(int groupNumber, Pattern then, Pattern otherwise)
{
return _precedingPattern.RegEx(String.Format("(?({0}){1}|{2})", groupNumber, then, otherwise));
}
}
}