简体   繁体   English

修改正则表达式以同时匹配IP地址

[英]Modify Regex to Also Match IP Address

I currently use this regular expression to validate URLs: 我目前使用此正则表达式来验证URL:

^([a-z0-9+!*(),;?&=\\$_.-]+(\\:[a-z0-9+!*(),;?&=\\$_.-]+)?@)?([a-z0-9-.]*)\\.([az]{2,4})(\\:0*(?: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}|[1-9][0-9]{1,3}|[0-9]))?(\\/([a-z0-9+\\$_-]\\.?)+)*\\/?(\\?[a-z+&\\$_.-][a-z0-9;:@&%=+\\/\\$_.-]*)?(#[a-z_.-][a-z0-9+\\$_.-]*)?$

It matches a fairly long list of URLs: 它与相当长的URL列表匹配:

google.com
google.com#a1
google.com?abc=123
google.com:80
google.com:80#a1
google.com:80?abc=123
google.com:80/test
google.com:80/test#a1
google.com:80/test?abc=123
google.com:80/test?abc=123#a1
www.google.com
www.google.com#a1
www.google.com?abc=123
www.google.com:80
www.google.com:80#a1
www.google.com:80?abc=123
www.google.com:80/test
www.google.com:80/test#a1
www.google.com:80/test?abc=123
www.google.com:80/test?abc=123#a1
www.www.google.com
www.www.google.com#a1
www.www.google.com?abc=123
www.www.google.com:80
www.www.google.com:80#a1
www.www.google.com:80?abc=123
www.www.google.com:80/test
www.www.google.com:80/test#a1
www.www.google.com:80/test?abc=123
www.www.google.com:80/test?abc=123#a1
john:smith@google.com
john:smith@google.com#a1
john:smith@google.com?abc=123
john:smith@google.com:80
john:smith@google.com:80#a1
john:smith@google.com:80?abc=123
john:smith@google.com:80/test
john:smith@google.com:80/test#a1
john:smith@google.com:80/test?abc=123
john:smith@google.com:80/test?abc=123#a1
john:smith@www.google.com
john:smith@www.google.com#a1
john:smith@www.google.com?abc=123
john:smith@www.google.com:80
john:smith@www.google.com:80#a1
john:smith@www.google.com:80?abc=123
john:smith@www.google.com:80/test
john:smith@www.google.com:80/test#a1
john:smith@www.google.com:80/test?abc=123
john:smith@www.google.com:80/test?abc=123#a1
john:smith@www.www.google.com
john:smith@www.www.google.com#a1
john:smith@www.www.google.com?abc=123
john:smith@www.www.google.com:80
john:smith@www.www.google.com:80#a1
john:smith@www.www.google.com:80?abc=123
john:smith@www.www.google.com:80/test
john:smith@www.www.google.com:80/test#a1
john:smith@www.www.google.com:80/test?abc=123
john:smith@www.www.google.com:80/test?abc=123#a1

However, it does not match these URLs which, to my knowledge, are also valid: 但是,与我所知也是有效的这些URL不匹配:

8.8.8.8
8.8.8.8#a1
8.8.8.8?abc=123
8.8.8.8:80
8.8.8.8:80#a1
8.8.8.8:80?abc=123
8.8.8.8:80/test
8.8.8.8:80/test#a1
8.8.8.8:80/test?abc=123
8.8.8.8:80/test?abc=123#a1
john:smith@8.8.8.8
john:smith@8.8.8.8#a1
john:smith@8.8.8.8?abc=123
john:smith@8.8.8.8:80
john:smith@8.8.8.8:80#a1
john:smith@8.8.8.8:80?abc=123
john:smith@8.8.8.8:80/test
john:smith@8.8.8.8:80/test#a1
john:smith@8.8.8.8:80/test?abc=123
john:smith@8.8.8.8:80/test?abc=123#a1

For reference, I found this one for IP addresses which seems to work well: 作为参考,我发现此地址适用于IP地址,该地址似乎运行良好:

^(?:(?: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]?)

How can I tie them together? 如何将它们绑在一起? Or, is there a better regex to match all the URLs here? 或者,是否有更好的正则表达式来匹配此处的所有URL?

Demo: 演示:

http://rubular.com/r/ufuNkHqX5G http://rubular.com/r/ufuNkHqX5G

You can combine two regular expressions together by doing (?:<regex1>|<regex2>) , which means whatever matches regex1 or regex2 . 您可以通过执行(?:<regex1>|<regex2>)将两个正则表达式组合在一起,这意味着可以匹配regex1regex2 (The ?: means the added parentheses won't capture). ?:表示不会捕获添加的括号)。

You can find a variety of regexes for URL validation online, eg In search of the perfect URL validation regex lists quite a few. 您可以在线找到各种用于URL验证的正则表达式 ,例如, 在搜索完美的URL验证时,正则表达式列出了很多。

Validating an email address is a bit more complicated than validating a webpage URL. 验证电子邮件地址比验证网页URL更为复杂。 In fact, determining a proper regex for validating an email address seems to be a question without one definitive right answer; 实际上,确定一个合适的正则表达式来验证电子邮件地址似乎是一个没有明确答案的问题。 see Using a regular expression to validate an email address 请参阅使用正则表达式来验证电子邮件地址

If you use PHP, you aren't limited to using a regex to validate email addresses and URLs, as the following code illustrates: 如果您使用PHP,则不限于使用正则表达式来验证电子邮件地址和URL,如以下代码所示:

<?php
$url = "http://8.8.8.8";
$mess = (!filter_var($url, FILTER_VALIDATE_URL))? "invalid" : "valid";
echo $mess, ": $url\n";

$email = "me@he re.com";
$mess = (!filter_var($email, FILTER_VALIDATE_EMAIL))? "invalid" :"valid";
echo $mess, ": $email\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM