@@ -5,12 +5,22 @@ namespace NETworkManager.Utilities;
55public static partial class RegexHelper
66{
77 /// <summary>
8- /// Match an IPv4-Address like 192.168.178.1
9- /// </summary>
10- // ReSharper disable once InconsistentNaming
8+ /// Represents a regular expression pattern that matches valid IPv4 address values.
9+ /// </summary>
1110 private const string IPv4AddressValues =
1211 @"((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]?)" ;
1312
13+ /// <summary>
14+ /// Represents a regular expression pattern that matches valid IPv4 subnet mask values.
15+ /// </summary>
16+ private const string SubnetmaskValues =
17+ @"(((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}))" ;
18+
19+ /// <summary>
20+ /// Represents the regular expression pattern used to validate CIDR notation values for IPv4 subnet masks.
21+ /// </summary>
22+ private const string CidrRegexValues = @"([1-9]|[1-2][0-9]|3[0-2])" ;
23+
1424 /// <summary>
1525 /// Provides a compiled regular expression that matches valid IPv4 addresses in dot-decimal notation.
1626 /// </summary>
@@ -43,31 +53,59 @@ public static partial class RegexHelper
4353 [ GeneratedRegex ( $ "^{ IPv4AddressValues } -{ IPv4AddressValues } $") ]
4454 public static partial Regex IPv4AddressRangeRegex ( ) ;
4555
56+ /// <summary>
57+ /// Provides a compiled regular expression that matches valid IPv4 subnet mask values.
58+ /// </summary>
59+ /// <remarks>The returned regular expression is generated at compile time and is optimized for
60+ /// performance. Use this regex to validate or parse subnet mask strings in IPv4 networking scenarios.</remarks>
61+ /// <returns>A <see cref="Regex"/> instance that matches strings representing valid IPv4 subnet masks.</returns>
62+ [ GeneratedRegex ( $ "^{ SubnetmaskValues } $") ]
63+ public static partial Regex SubnetmaskRegex ( ) ;
64+
65+ /// <summary>
66+ /// Provides a compiled regular expression that matches IPv4 addresses with subnet masks in CIDR notation, such as
67+ /// "192.168.178.0/255.255.255.0".
68+ /// </summary>
69+ /// <remarks>The returned regular expression validates both the IPv4 address and the subnet mask
70+ /// components. Use this regex to verify input strings representing IPv4 subnets in formats like
71+ /// "address/mask".</remarks>
72+ /// <returns>A <see cref="Regex"/> instance that matches strings containing an IPv4 address followed by a subnet mask,
73+ /// separated by a forward slash.</returns>
74+ [ GeneratedRegex ( $@ "^{ IPv4AddressValues } \/{ SubnetmaskValues } $") ]
75+ public static partial Regex IPv4AddressSubnetmaskRegex ( ) ;
76+
77+ /// <summary>
78+ /// Provides a compiled regular expression that matches an IPv4 address in CIDR notation, such as
79+ /// "192.168.178.0/24".
80+ /// </summary>
81+ /// <remarks>The returned regular expression can be used to validate or extract IPv4 addresses with CIDR
82+ /// notation, such as "192.168.1.0/24". The pattern enforces correct formatting for both the address and the prefix
83+ /// length.</remarks>
84+ /// <returns>A <see cref="Regex"/> instance that matches strings containing an IPv4 address followed by a slash and a valid
85+ /// CIDR prefix length.</returns>
86+ [ GeneratedRegex ( $@ "^{ IPv4AddressValues } \/{ CidrRegexValues } $") ]
87+ public static partial Regex IPv4AddressCidrRegex ( ) ;
88+
89+ /// <summary>
90+ /// Creates a regular expression that matches IPv4 addresses, allowing for a special range in one or more octets.
91+ /// </summary>
92+ /// <remarks>The returned regular expression matches standard IPv4 addresses and addresses where one or
93+ /// more octets are defined by a custom range pattern. This is useful for validating or parsing addresses such as
94+ /// "192.168.[50-100].1" where a range is specified in place of an octet. The format and behavior of the special
95+ /// range are determined by the <c>SpecialRangeRegex</c> value.</remarks>
96+ /// <returns>A <see cref="Regex"/> instance that matches IPv4 addresses with support for custom special ranges as defined by
97+ /// <c>SpecialRangeRegex</c>.</returns>
98+ [ GeneratedRegex ( $@ "^(?:(?: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 } )$") ]
99+ public static partial Regex IPv4AddressSpecialRangeRegex ( ) ;
100+
46101 // Match a MAC-Address 000000000000 00:00:00:00:00:00, 00-00-00-00-00-00-00 or 0000.0000.0000
47102 public const string MACAddressRegex =
48103 @"^^[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}$$" ;
49104
50105 // Match the first 3 bytes of a MAC-Address 000000, 00:00:00, 00-00-00
51106 public const string MACAddressFirst3BytesRegex =
52107 @"^[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}$" ;
53-
54- // Private subnetmask / cidr values
55- private const string SubnetmaskValues =
56- @"(((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}))" ;
57-
58- private const string CidrRegex = @"([1-9]|[1-2][0-9]|3[0-2])" ;
59-
60- // Match a Subnetmask like 255.255.255.0
61- public const string SubnetmaskRegex = @"^" + SubnetmaskValues + @"$" ;
62-
63- // Match a subnet from 192.168.178.0/1 to 192.168.178.0/32
64- // ReSharper disable once InconsistentNaming
65- public const string IPv4AddressCidrRegex = $@ "^{ IPv4AddressValues } \/{ CidrRegex } $";
66-
67- // Match a subnet from 192.168.178.0/192.0.0.0 to 192.168.178.0/255.255.255.255
68- // ReSharper disable once InconsistentNaming
69- public const string IPv4AddressSubnetmaskRegex = $@ "^{ IPv4AddressValues } \/{ SubnetmaskValues } $";
70-
108+
71109 // Match IPv6 address like ::1
72110 // ReSharper disable once InconsistentNaming
73111 public const string IPv6AddressRegex =
@@ -82,11 +120,6 @@ public static partial class RegexHelper
82120 public const string SpecialRangeRegex =
83121 @"\[((?: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]?))))*\]" ;
84122
85- // Match a IPv4-Address like 192.168.[50-100].1
86- // ReSharper disable once InconsistentNaming
87- public const string IPv4AddressSpecialRangeRegex =
88- $@ "^(?:(?: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 } )$";
89-
90123 // Private hostname values
91124 private const string HostnameOrDomainValues =
92125 @"(?=.{1,255}$)(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.[A-Za-z0-9-]{1,63})*\.?" ;
@@ -95,7 +128,7 @@ public static partial class RegexHelper
95128 public const string HostnameOrDomainRegex = $@ "^{ HostnameOrDomainValues } $";
96129
97130 // Match a hostname with cidr like server-01.example.com/24
98- public const string HostnameOrDomainWithCidrRegex = $@ "^{ HostnameOrDomainValues } \/{ CidrRegex } $";
131+ public const string HostnameOrDomainWithCidrRegex = $@ "^{ HostnameOrDomainValues } \/{ CidrRegexValues } $";
99132
100133 // Match a hostname with subnetmask like server-01.example.com/255.255.255.0
101134 public const string HostnameOrDomainWithSubnetmaskRegex = $@ "^{ HostnameOrDomainValues } \/{ SubnetmaskValues } $";
0 commit comments