博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
正则匹配IP
阅读量:5030 次
发布时间:2019-06-12

本文共 3129 字,大约阅读时间需要 10 分钟。

 

 Aha, but you see, all this time diving into regular expressions

was a mistake.
Because we failed to figure out
.
This was a case of somebody “solving” half of their problem
and then asking for help with the other half:
“I have a string and I want to check whether it is a dotted decimal
IPv4 address.
I know, I’ll write a regular expression!
Hey, can anybody help me write this regular expression?”

The real problem was not “How do I write a regular expression to

recognize a dotted decimal IPv4 address.”
The real problem was simply “How do I recognize a dotted decimal IPv4
address.”
And with this broader goal in mind, you recognize that limiting
yourself to a regular expression only made the problem harder.

function isDottedIPv4(s){ var match = s.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); return match != null &&        match[1] <= 255 && match[2] <= 255 &&        match[3] <= 255 && match[4] <= 255;}WScript.StdOut.WriteLine(isDottedIPv4("127.0.0.001"));WScript.StdOut.WriteLine(isDottedIPv4("448.90210.0.65535"));WScript.StdOut.WriteLine(isDottedIPv4("microsoft.com"));

And this was just a simple dotted decimal IPv4 address.

Woe unto you if you decide you want to
.

Don’t make regular expressions do what they’re not good at.

If you want to match a simple pattern, then match a simple pattern.
If you want to do math, then do math.
As commenter Maurits put it,
“.

 

 

The limitation with method is that it verifies if a string could be converted to IP address, thus if it is supplied with a string value like "5", it consider it as "0.0.0.5".

Another approach to validate an IPv4 could be following :

public bool ValidateIPv4(string ipString) { if (String.IsNullOrWhiteSpace(ipString)) { return false; } string[] splitValues = ipString.Split('.'); if (splitValues.Length != 4) { return false; } byte tempForParsing; return splitValues.All(r => byte.TryParse(r, out tempForParsing)); }

It could be tested like:

List
ipAddresses = new List
{ "2", "1.2.3", "1.2.3.4", "255.256.267.300", "127.0.0.1", }; foreach (var ip in ipAddresses) { Console.WriteLine($"{ip} ==> {ValidateIPv4(ip)}"); }

The output will be:

2 ==> False 1.2.3 ==> False 1.2.3.4 ==> True 255.256.267.300 ==> False 127.0.0.1 ==> True

 

 

You can also use IPAddress.TryParse but it has the limitations and could result in incorrect parsing.

Note that TryParse returns true if it parsed the input successfully, but that this does not necessarily mean that the resulting IP address is a valid one. Do not use this method to validate IP addresses.

But this would work with normal string containing at least three dots. Something like:

string addrString = "192.168.0.1"; IPAddress address; if (IPAddress.TryParse(addrString, out address)) { //Valid IP, with address containing the IP } else { //Invalid IP }

With IPAddress.TryParse you can check for existence of three dots and then call TryParse like:

public static bool ValidateIPv4(string ipString) { if (ipString.Count(c => c == '.') != 3) return false; IPAddress address; return IPAddress.TryParse(ipString, out address); }

 

转载于:https://www.cnblogs.com/chucklu/p/10775881.html

你可能感兴趣的文章
傅里叶级数与积分方程
查看>>
软工作业3:用户体验分析——以“南通大学教务管理系统微信公众号”为例
查看>>
Css:背景色透明,内容不透明之终极方法!兼容所有浏览器
查看>>
我们前端跟后端是怎么合作的
查看>>
mysql存储过程
查看>>
洛谷P2556 [AHOI2002] 黑白图像压缩 [模拟]
查看>>
letecode [136] - Single Number
查看>>
linux下设置固定IP的方法
查看>>
VMware虚拟机下Linux系统的全屏显示
查看>>
net core体系-web应用程序-4asp.net core2.0 项目实战(任务管理系统)-2项目搭建
查看>>
高效的jQuery
查看>>
ubuntu 16.04 (软件应用)-输入法
查看>>
windos7修复引导扇区
查看>>
Leetcode总结之Backtracking
查看>>
Android开发学习之路-图片颜色获取器开发(1)
查看>>
StackExchange.Redis 官方文档(一) Basics
查看>>
nupkg 之破解 nodejs+electron-packager 打包exe的解包
查看>>
Objective-C 使用 C++类
查看>>
浅谈之高级查询over(partition by)
查看>>
Notes: CRM Analytics–BI from a CRM perspective (2)
查看>>