Skip to content

Commit 20bd604

Browse files
committed
Chore: Adjust regex for IPv4
1 parent 37d7744 commit 20bd604

File tree

13 files changed

+68
-49
lines changed

13 files changed

+68
-49
lines changed

Source/NETworkManager.Converters/StringIsNotNullOrEmptyOrIPv4AddressToBooleanConverter.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
using System;
1+
using NETworkManager.Utilities;
2+
using System;
23
using System.Globalization;
3-
using System.Text.RegularExpressions;
44
using System.Windows.Data;
5-
using NETworkManager.Utilities;
65

76
namespace NETworkManager.Converters;
87

98
public sealed class StringIsNotNullOrEmptyOrIPv4AddressToBooleanConverter : IValueConverter
109
{
1110
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
1211
{
13-
return !string.IsNullOrEmpty(value as string) && !Regex.IsMatch((string)value, RegexHelper.IPv4AddressRegex);
12+
return !string.IsNullOrEmpty(value as string) && !RegexHelper.IPv4AddressRegex().IsMatch((string)value);
1413
}
1514

1615
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

Source/NETworkManager.Models/Network/HostRangeHelper.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using System.Collections.Concurrent;
1+
using ControlzEx.Standard;
2+
using NETworkManager.Utilities;
3+
using System.Collections.Concurrent;
24
using System.Collections.Generic;
35
using System.Linq;
46
using System.Net;
57
using System.Net.Sockets;
68
using System.Text.RegularExpressions;
79
using System.Threading;
810
using System.Threading.Tasks;
9-
using NETworkManager.Utilities;
1011

1112
namespace NETworkManager.Models.Network;
1213

@@ -46,7 +47,7 @@ private static (List<(IPAddress ipAddress, string hostname)> hosts, List<string>
4647
switch (host)
4748
{
4849
// 192.168.0.1
49-
case var _ when Regex.IsMatch(host, RegexHelper.IPv4AddressRegex):
50+
case var _ when RegexHelper.IPv4AddressRegex().IsMatch(host):
5051
// 2001:db8:85a3::8a2e:370:7334
5152
case var _ when Regex.IsMatch(host, RegexHelper.IPv6AddressRegex):
5253
hostsBag.Add((IPAddress.Parse(host), string.Empty));
@@ -71,7 +72,7 @@ private static (List<(IPAddress ipAddress, string hostname)> hosts, List<string>
7172
break;
7273

7374
// 192.168.0.0 - 192.168.0.100
74-
case var _ when Regex.IsMatch(host, RegexHelper.IPv4AddressRangeRegex):
75+
case var _ when RegexHelper.IPv4AddressRangeRegex().IsMatch(host):
7576
var range = host.Split('-');
7677

7778
Parallel.For(IPv4Address.ToInt32(IPAddress.Parse(range[0])),

Source/NETworkManager.Models/Network/SNTPLookup.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using System;
1+
using ControlzEx.Standard;
2+
using NETworkManager.Utilities;
3+
using System;
24
using System.Collections.Generic;
35
using System.Net;
46
using System.Net.Sockets;
57
using System.Text.RegularExpressions;
68
using System.Threading.Tasks;
7-
using NETworkManager.Utilities;
89

910
namespace NETworkManager.Models.Network;
1011

@@ -102,7 +103,7 @@ public void QueryAsync(IEnumerable<ServerConnectionInfo> servers, bool dnsResolv
102103
// NTP requires an IP address to connect to
103104
IPAddress serverIP = null;
104105

105-
if (Regex.IsMatch(server.Server, RegexHelper.IPv4AddressRegex) ||
106+
if (RegexHelper.IPv4AddressRegex().IsMatch(server.Server) ||
106107
Regex.IsMatch(server.Server, RegexHelper.IPv6AddressRegex))
107108
{
108109
serverIP = IPAddress.Parse(server.Server);

Source/NETworkManager.Utilities/RegexHelper.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
namespace NETworkManager.Utilities;
1+
using System.Text.RegularExpressions;
22

3-
public static class RegexHelper
3+
namespace NETworkManager.Utilities;
4+
5+
public static partial class RegexHelper
46
{
57
/// <summary>
68
/// Match an IPv4-Address like 192.168.178.1
@@ -10,20 +12,36 @@ public static class RegexHelper
1012
@"((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]?)";
1113

1214
/// <summary>
13-
/// Match exactly an IPv4-Address like 192.168.178.1
15+
/// Provides a compiled regular expression that matches valid IPv4 addresses in dot-decimal notation.
1416
/// </summary>
15-
// ReSharper disable once InconsistentNaming
16-
public const string IPv4AddressRegex = $"^{IPv4AddressValues}$";
17+
/// <remarks>The returned regular expression is compiled for performance. Use this regex to validate or
18+
/// extract IPv4 addresses from text. The pattern enforces correct octet ranges and dot separators.</remarks>
19+
/// <returns>A <see cref="Regex"/> instance that matches IPv4 addresses in the format "x.x.x.x", where each x is a number
20+
/// from 0 to 255.</returns>
21+
[GeneratedRegex($"^{IPv4AddressValues}$")]
22+
public static partial Regex IPv4AddressRegex();
1723

1824
/// <summary>
19-
/// Match IPv4-Address within a string
25+
/// Provides a compiled regular expression that matches valid IPv4 addresses within input text.
2026
/// </summary>
21-
// ReSharper disable once InconsistentNaming
22-
public const string IPv4AddressExtractRegex = IPv4AddressValues;
27+
/// <remarks>The returned regular expression matches IPv4 addresses in standard dotted-decimal notation
28+
/// (e.g., "192.168.1.1"). The regular expression is compiled for improved performance when used
29+
/// repeatedly.</remarks>
30+
/// <returns>A <see cref="Regex"/> instance that can be used to extract IPv4 addresses from strings.</returns>
31+
[GeneratedRegex(IPv4AddressValues)]
32+
public static partial Regex IPv4AddressExtractRegex();
2333

24-
// Match IPv4-Address Range like 192.168.178.1-192.168.178.127
25-
// ReSharper disable once InconsistentNaming
26-
public const string IPv4AddressRangeRegex = $"^{IPv4AddressValues}-{IPv4AddressValues}$";
34+
/// <summary>
35+
/// Gets a regular expression that matches an IPv4 address range in the format "start-end", where both start and end
36+
/// are valid IPv4 addresses.
37+
/// </summary>
38+
/// <remarks>The regular expression expects the input to consist of two IPv4 addresses separated by a
39+
/// hyphen, with no additional whitespace or characters. Both addresses must be valid IPv4 addresses. This can be
40+
/// used to validate or parse address range strings in network configuration scenarios.</remarks>
41+
/// <returns>A <see cref="Regex"/> instance that matches strings representing IPv4 address ranges, such as
42+
/// "192.168.1.1-192.168.1.100".</returns>
43+
[GeneratedRegex($"^{IPv4AddressValues}-{IPv4AddressValues}$")]
44+
public static partial Regex IPv4AddressRangeRegex();
2745

2846
// Match a MAC-Address 000000000000 00:00:00:00:00:00, 00-00-00-00-00-00-00 or 0000.0000.0000
2947
public const string MACAddressRegex =

Source/NETworkManager.Validators/EmptyOrIPv4AddressValidator.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using System.Globalization;
2-
using System.Text.RegularExpressions;
3-
using System.Windows.Controls;
1+
using ControlzEx.Standard;
42
using NETworkManager.Localization.Resources;
53
using NETworkManager.Utilities;
4+
using System.Globalization;
5+
using System.Text.RegularExpressions;
6+
using System.Windows.Controls;
67

78
namespace NETworkManager.Validators;
89

@@ -13,7 +14,7 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
1314
if (string.IsNullOrEmpty(value as string))
1415
return ValidationResult.ValidResult;
1516

16-
return Regex.IsMatch((string)value, RegexHelper.IPv4AddressRegex)
17+
return RegexHelper.IPv4AddressRegex().IsMatch((string)value)
1718
? ValidationResult.ValidResult
1819
: new ValidationResult(false, Strings.EnterValidIPv4Address);
1920
}

Source/NETworkManager.Validators/IPAddressOrHostnameAsRangeValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
2020
var localItem = item.Trim();
2121

2222
// Check if it is a valid IPv4 address like 192.168.0.1, a valid IPv6 address like ::1 or a valid hostname like server-01 or server-01.example.com
23-
var isValid = Regex.IsMatch(localItem, RegexHelper.IPv4AddressRegex) ||
23+
var isValid = RegexHelper.IPv4AddressRegex().IsMatch(localItem) ||
2424
Regex.IsMatch(localItem, RegexHelper.IPv6AddressRegex) ||
2525
Regex.IsMatch(localItem, RegexHelper.HostnameOrDomainRegex);
2626

Source/NETworkManager.Validators/IPAddressOrHostnameValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
1616
return new ValidationResult(false, Strings.EnterValidHostnameOrIPAddress);
1717

1818
// Check if it is a valid IPv4 address like 192.168.0.1
19-
if (Regex.IsMatch(input, RegexHelper.IPv4AddressRegex))
19+
if (RegexHelper.IPv4AddressRegex().IsMatch(input))
2020
return ValidationResult.ValidResult;
2121

2222
// Check if it is a valid IPv6 address like ::1

Source/NETworkManager.Validators/IPv4AddressValidator.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using System.Globalization;
2-
using System.Text.RegularExpressions;
3-
using System.Windows.Controls;
4-
using NETworkManager.Localization.Resources;
1+
using NETworkManager.Localization.Resources;
52
using NETworkManager.Utilities;
3+
using System.Globalization;
4+
using System.Windows.Controls;
65

76
namespace NETworkManager.Validators;
87

@@ -15,7 +14,7 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
1514
if (string.IsNullOrEmpty(ipAddress))
1615
return new ValidationResult(false, Strings.EnterValidIPv4Address);
1716

18-
return Regex.IsMatch(ipAddress, RegexHelper.IPv4AddressRegex)
17+
return RegexHelper.IPv4AddressRegex().IsMatch(ipAddress)
1918
? ValidationResult.ValidResult
2019
: new ValidationResult(false, Strings.EnterValidIPv4Address);
2120
}

Source/NETworkManager.Validators/MultipleHostsRangeValidator.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using System.Globalization;
1+
using NETworkManager.Localization.Resources;
2+
using NETworkManager.Models.Network;
3+
using NETworkManager.Utilities;
4+
using System.Globalization;
25
using System.Net;
36
using System.Text.RegularExpressions;
47
using System.Windows.Controls;
5-
using NETworkManager.Localization.Resources;
6-
using NETworkManager.Models.Network;
7-
using NETworkManager.Utilities;
88

99
namespace NETworkManager.Validators;
1010

@@ -20,7 +20,7 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
2020
foreach (var ipHostOrRange in ((string)value).Replace(" ", "").Split(';'))
2121
{
2222
// 192.168.0.1
23-
if (Regex.IsMatch(ipHostOrRange, RegexHelper.IPv4AddressRegex))
23+
if (RegexHelper.IPv4AddressRegex().IsMatch(ipHostOrRange))
2424
continue;
2525

2626
// 192.168.0.0/24
@@ -32,7 +32,7 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
3232
continue;
3333

3434
// 192.168.0.0 - 192.168.0.100
35-
if (Regex.IsMatch(ipHostOrRange, RegexHelper.IPv4AddressRangeRegex))
35+
if (RegexHelper.IPv4AddressRangeRegex().IsMatch(ipHostOrRange))
3636
{
3737
var range = ipHostOrRange.Split('-');
3838

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using System.Globalization;
1+
using NETworkManager.Localization.Resources;
2+
using NETworkManager.Utilities;
3+
using System.Globalization;
24
using System.Text.RegularExpressions;
35
using System.Windows.Controls;
4-
using NETworkManager.Localization.Resources;
5-
using NETworkManager.Utilities;
66

77
namespace NETworkManager.Validators;
88

@@ -15,13 +15,13 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
1515

1616
for (var index = 0; index < ((string)value).Split(';').Length; index++)
1717
{
18-
var ipAddress = ((string)value).Split(';')[index];
18+
var ipAddress = ((string)value).Split(';')[index].Trim();
1919

20-
if (!Regex.IsMatch(ipAddress.Trim(), RegexHelper.IPv4AddressRegex) &&
20+
if (!RegexHelper.IPv4AddressRegex().IsMatch(ipAddress) &&
2121
!Regex.IsMatch(ipAddress.Trim(), RegexHelper.IPv6AddressRegex))
2222
return new ValidationResult(false, Strings.EnterOneOrMoreValidIPAddresses);
2323
}
2424

2525
return ValidationResult.ValidResult;
2626
}
27-
}
27+
}

0 commit comments

Comments
 (0)