3 ms·
.NET has something close in standard libraries, in that you can try converting a string to System.Net.IPAddress and other similar classes. It's for parsing rat
by andy81 4y ago
.NET has something close in standard libraries, in that you can try converting a string to System.Net.IPAddress and other similar classes.
It's for parsing rather than search though, you'd still need regex if e.g. searching unstructured logs for IPs.
e.g. (powershell syntax for easy reproduction) returns parsed, or throw if invalid.
[uri]"\\ServerName1\Folder\UserName"
[version]'1.2.3.4'
[ipaddress]'1.1.1.1'
- gregmac 4y agoMany of these have a `TryParse` method [1] which makes for nice syntax, and of course then you get a variable of the correct type. if (int.TryParse(userInput, out var number)) { total += number; } This is cleaner code compared to try/catch and usually faster (though debatable if it matters) [2]. [1] https://learn.microsoft.com/en-us/dotnet/api/system.net.ipaddress.tryparse?view=net-7.0 https://learn.microsoft.com/en-us/dotnet/api/system.net.ipad... [2] https://stackoverflow.com/q/161942 https://stackoverflow.com/q/161942