forked from BornToBeRoot/NETworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegexHelper.cs
More file actions
130 lines (98 loc) · 8.34 KB
/
Copy pathRegexHelper.cs
File metadata and controls
130 lines (98 loc) · 8.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
120
121
122
123
124
125
126
127
128
129
130
namespace NETworkManager.Utilities;
public static class RegexHelper
{
/// <summary>
/// Match an IPv4-Address like 192.168.178.1
/// </summary>
// ReSharper disable once InconsistentNaming
private const string IPv4AddressValues =
@"((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
/// <summary>
/// Match exactly an IPv4-Address like 192.168.178.1
/// </summary>
// ReSharper disable once InconsistentNaming
public const string IPv4AddressRegex = $"^{IPv4AddressValues}$";
/// <summary>
/// Match IPv4-Address within a string
/// </summary>
// ReSharper disable once InconsistentNaming
public const string IPv4AddressExtractRegex = IPv4AddressValues;
// Match IPv4-Address Range like 192.168.178.1-192.168.178.127
// ReSharper disable once InconsistentNaming
public const string IPv4AddressRangeRegex = $"^{IPv4AddressValues}-{IPv4AddressValues}$";
// Match a MAC-Address 000000000000 00:00:00:00:00:00, 00-00-00-00-00-00-00 or 0000.0000.0000
public const string MACAddressRegex =
@"^^[A-Fa-f0-9]{12}$|^[A-Fa-f0-9]{2}(:|-){1}[A-Fa-f0-9]{2}(:|-){1}[A-Fa-f0-9]{2}(:|-){1}[A-Fa-f0-9]{2}(:|-){1}[A-Fa-f0-9]{2}(:|-){1}[A-Fa-f0-9]{2}$|^[A-Fa-f0-9]{4}.[A-Fa-f0-9]{4}.[A-Fa-f0-9]{4}$$";
// Match the first 3 bytes of a MAC-Address 000000, 00:00:00, 00-00-00
public const string MACAddressFirst3BytesRegex =
@"^[A-Fa-f0-9]{6}$|^[A-Fa-f0-9]{2}(:|-){1}[A-Fa-f0-9]{2}(:|-){1}[A-Fa-f0-9]{2}$|^[A-Fa-f0-9]{4}.[A-Fa-f0-9]{2}$";
// Private subnetmask / cidr values
private const string SubnetmaskValues =
@"(((255\.){3}(255|254|252|248|240|224|192|128|0+))|((255\.){2}(255|254|252|248|240|224|192|128|0+)\.0)|((255\.)(255|254|252|248|240|224|192|128|0+)(\.0+){2})|((255|254|252|248|240|224|192|128|0+)(\.0+){3}))";
private const string CidrRegex = @"([1-9]|[1-2][0-9]|3[0-2])";
// Match a Subnetmask like 255.255.255.0
public const string SubnetmaskRegex = @"^" + SubnetmaskValues + @"$";
// Match a subnet from 192.168.178.0/1 to 192.168.178.0/32
// ReSharper disable once InconsistentNaming
public const string IPv4AddressCidrRegex = $@"^{IPv4AddressValues}\/{CidrRegex}$";
// Match a subnet from 192.168.178.0/192.0.0.0 to 192.168.178.0/255.255.255.255
// ReSharper disable once InconsistentNaming
public const string IPv4AddressSubnetmaskRegex = $@"^{IPv4AddressValues}\/{SubnetmaskValues}$";
// Match IPv6 address like ::1
// ReSharper disable once InconsistentNaming
public const string IPv6AddressRegex =
@"(?:^|(?<=\s))(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(?=\s|$)";
// Match a subnet like 2001:0db8::/64
// ReSharper disable once InconsistentNaming
public const string IPv6AddressCidrRegex =
@"^s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8])){1}?$";
// Match a range like [0-255], [0,2,4] and [2,4-6]
public const string SpecialRangeRegex =
@"\[((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)-(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))([,]((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)-(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))))*\]";
// Match a IPv4-Address like 192.168.[50-100].1
// ReSharper disable once InconsistentNaming
public const string IPv4AddressSpecialRangeRegex =
$@"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|{SpecialRangeRegex})\.){{3}}((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|{SpecialRangeRegex})$";
// Private hostname values
private const string HostnameOrDomainValues =
@"(?=.{1,255}$)(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.[A-Za-z0-9-]{1,63})*\.?";
// Hostname regex like server-01 or server-01.example.com
public const string HostnameOrDomainRegex = $@"^{HostnameOrDomainValues}$";
// Match a hostname with cidr like server-01.example.com/24
public const string HostnameOrDomainWithCidrRegex = $@"^{HostnameOrDomainValues}\/{CidrRegex}$";
// Match a hostname with subnetmask like server-01.example.com/255.255.255.0
public const string HostnameOrDomainWithSubnetmaskRegex = $@"^{HostnameOrDomainValues}\/{SubnetmaskValues}$";
// Match a domain local.example.com
public const string DomainRegex =
@"^(?=.{1,255}$)[a-zA-Z0-9\-_]{1,63}((\.[a-zA-Z0-9\-_]+){1,63})*\.[a-zA-Z]{2,}\.?$";
// Private port values
private const string PortValues =
@"((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4}))";
// Match a port between 1-65535
public const string PortRegex = $@"^{PortValues}$";
// Match any filepath (like "c:\temp\") --> https://www.codeproject.com/Tips/216238/Regular-Expression-to-Validate-File-Path-and-Exten
public const string FilePathRegex = @"^(?:[\w]\:|\\)(\\[a-z_\-\s0-9\.]+)+$";
// Match any fullname (like "c:\temp\test.txt") --> https://www.codeproject.com/Tips/216238/Regular-Expression-to-Validate-File-Path-and-Exten
public const string FullNameRegex =
@"^(?:[\w]\:|\\)(\\[a-zA-Z0-9_\-\s\.()~!@#$%^&=+';,{}\[\]]+)+\.[a-zA-z0-9]{1,4}$";
// Match a string that doesn't end with ";" (like "80; ldap; ssh; 443")
public const string StringNotEndWithSemicolonRegex = @"^.*[^;]$";
// Match a number (like 12, 12.4, 12,3)
public const string NumberRegex = @"^\d+((\.|,)\d+)?$";
// Match an SNMP OID (like 1.3.6.1 or .1.3.6.2)
public const string SnmpOidRegex = @"^\.?[012]\.(?:[0-9]|[1-3][0-9])(\.\d+)*$";
// Match a hosts file entry with optional comments, supporting IPv4, IPv6, and hostnames
// ^* : Matches the beginning of the line
// (#)? : Optionally matches a comment (#) at the start of the line
// \s* : Matches any whitespace after the comment (or before the IP)
// ((?:(?:\d{1,3}\.){3}\d{1,3}) : Matches an IPv4 address (e.g., 192.168.1.1)
// | : OR (alternation between IPv4 and IPv6)
// (?:(?:[A-Fa-f0-9:]+:+)+[A-Fa-f0-9]+) : Matches an IPv6 address (e.g., 2001:db8::1)
// \s+ : Matches one or more spaces between the IP and the hostnames
// ([\w.-]+(?:\s+[\w.-]+)*) : Matches one or more hostnames, separated by spaces
// \s* : Matches optional whitespace after hostnames
// (#.*)? : Optionally matches a comment after hostnames
// $ : Anchors the match to the end of the line
public static string HostsEntryRegex =>
@"^(#)?\s*((?:(?:\d{1,3}\.){3}\d{1,3})|(?:(?:[A-Fa-f0-9:]+:+)+[A-Fa-f0-9]+))\s+([\w.-]+(?:\s+[\w.-]+)*)\s*(#.*)?$";
}