-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathContainerImageRegex.cs
More file actions
119 lines (103 loc) · 4.34 KB
/
ContainerImageRegex.cs
File metadata and controls
119 lines (103 loc) · 4.34 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
namespace Microsoft.ComponentDetection.Common;
using System.Linq;
using System.Text.RegularExpressions;
public static class ContainerImageRegex
{
public static readonly Regex AlphaNumericRegexp = new Regex("[a-z0-9]+");
public static readonly Regex SeparatorRegexp = new Regex("(?:[._]|__|[-]*)");
public static readonly Regex DomainComponentRegexp = new Regex("(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])");
public static readonly Regex TagRegexp = new Regex(@"[\w][\w.-]{0,127}");
public static readonly Regex DigestRegexp = new Regex("[a-zA-Z][a-zA-Z0-9]*(?:[-_+.][a-zA-Z][a-zA-Z0-9]*)*[:][a-fA-F0-9]{32,}");
public static readonly Regex IdentifierRegexp = new Regex("[a-f0-9]{64}");
public static readonly Regex NameComponentRegexp = Expression(
AlphaNumericRegexp,
Optional(Repeated(SeparatorRegexp, AlphaNumericRegexp)));
public static readonly Regex DomainRegexp = Expression(
DomainComponentRegexp,
Optional(
Repeated(
new Regex(@"\."),
DomainComponentRegexp)),
Optional(
new Regex(":"),
new Regex("[0-9]+")));
public static readonly Regex AnchoredDigestRegexp = Anchored(DigestRegexp);
public static readonly Regex NameRegexp = Expression(
Optional(
DomainRegexp,
new Regex(@"\/")),
NameComponentRegexp,
Optional(
Repeated(
new Regex(@"\/"),
NameComponentRegexp)));
public static readonly Regex AnchoredNameRegexp = Anchored(
Optional(
Capture(DomainRegexp),
new Regex(@"\/")),
Capture(
NameComponentRegexp,
Optional(
Repeated(
new Regex(@"\/"),
NameComponentRegexp))));
public static readonly Regex ReferenceRegexp = Anchored(
Capture(NameRegexp),
Optional(new Regex(":"), Capture(TagRegexp)),
Optional(new Regex("@"), Capture(DigestRegexp)));
public static readonly Regex AnchoredIdentifierRegexp = Anchored(IdentifierRegexp);
/// <summary>
/// expression defines a full expression, where each regular expression must follow the previous.
/// </summary>
/// <param name="regexps">list of Regular expressions.</param>
/// <returns> full Regex expression <see cref="Regex"/> from the given list. </returns>
public static Regex Expression(params Regex[] regexps)
{
return new Regex(string.Join(string.Empty, regexps.Select(re => re.ToString())));
}
/// <summary>
/// group wraps the regexp in a non-capturing group.
/// </summary>
/// <param name="regexps">list of Regular expressions.</param>
/// <returns> <see cref="Regex"/> of the non-capturing group. </returns>
public static Regex Group(params Regex[] regexps)
{
return new Regex($"(?:{Expression(regexps)})");
}
/// <summary>
/// repeated wraps the regexp in a non-capturing group to get one or more matches.
/// </summary>
/// <param name="regexps">list of Regular expressions.</param>
/// <returns> The wrapped <see cref="Regex"/>. </returns>
public static Regex Optional(params Regex[] regexps)
{
return new Regex($"{Group(regexps)}?");
}
/// <summary>
/// repeated wraps the regexp in a non-capturing group to get one or more matches.
/// </summary>
/// <param name="regexps">list of Regular expressions.</param>
/// <returns> The wrapped <see cref="Regex"/>. </returns>
public static Regex Repeated(params Regex[] regexps)
{
return new Regex($"{Group(regexps)}+");
}
/// <summary>
/// anchored anchors the regular expression by adding start and end delimiters.
/// </summary>
/// <param name="regexps">list of Regular expressions.</param>
/// <returns> The anchored <see cref="Regex"/>. </returns>
public static Regex Anchored(params Regex[] regexps)
{
return new Regex($"^{Expression(regexps)}$");
}
/// <summary>
/// capture wraps the expression in a capturing group.
/// </summary>
/// <param name="regexps">list of Regular expressions.</param>
/// <returns> The captured <see cref="Regex"/>. </returns>
public static Regex Capture(params Regex[] regexps)
{
return new Regex($"({Expression(regexps)})");
}
}