[英]How to validate this IP address using regex
i want to make regular expression to validate these type of ip addresses.like 我想做正则表达式来验证这些类型的IP地址。
192.168.12.1 # it may be any proper ip address
193.168.34.3-3 # this format should also be acceptable
Because i want to take value from user in range. 因为我想从用户范围内获取价值。
Thanks in advance 提前致谢
^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.
([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$
is the regex for validating ip addresses in 0-255.0-255.0-255.0-255
format. 是用于验证
0-255.0-255.0-255.0-255
格式的IP地址的正则表达式。 What is the range for the last number, the one separated with -
最后一个数字的范围是多少,用
-
隔开
EDIT 编辑
^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?)(?:\-([01]?\d\d?|2[0-4]\d|25[0-5]))?|2[0-4]\d|25[0-5](?:\-([01]?\d\d?|2[0-4]\d|25[0-5]))?$
This should work for OP's scenario. 这应该适用于OP的情况。
The Rails way to validate with ActiveRecord in Rails 3 is: 在Rails 3中使用ActiveRecord进行验证的Rails方法是:
@ip_regex = /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/
validates :gateway,
:presence => true,
:uniqueness => true,
:format => { :with => @ip_regex }
([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}[\\-0-9]*)
将验证您的两个示例。.如果您需要更具体的示例,是否可以更具体?
呼叫([0-9] | [1-9][0-9] | 1[0-9][0-9] | 2[0-4][0-9] | 25[0-5])
为简单起见,“正则表达式”为“模式”;
Pattern\.Pattern\.Pattern\.Pattern(-Pattern)?
You can start here 你可以从这里开始
http://answers.oreilly.com/topic/318-how-to-match-ipv4-addresses-with-regular-expressions/ http://answers.oreilly.com/topic/318-how-to-match-ipv4-addresses-with-regular-expressions/
and modify as you want. 并根据需要进行修改。
签出此正则表达式。
/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}(?:\-([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]))?$/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.