Java Regex match IP address

Reference:

[1] https://www.mkyong.com/regular-expressions/how-to-validate-ip-address-with-regular-expression/

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IPAddressValidator{

    private Pattern pattern;
    private Matcher matcher;

    private static final String IPADDRESS_PATTERN =
		"^([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])$";

    public IPAddressValidator(){
	  pattern = Pattern.compile(IPADDRESS_PATTERN);
    }

   /**
    * Validate ip address with regular expression
    * @param ip ip address for validation
    * @return true valid ip address, false invalid ip address
    */
    public boolean validate(final String ip){
	  matcher = pattern.matcher(ip);
	  return matcher.matches();
    }
}

Description

^		#start of the line
 (		#  start of group #1
   [01]?\\d\\d? #    Can be one or two digits. If three digits appear, it must start either 0 or 1
		#    e.g ([0-9], [0-9][0-9],[0-1][0-9][0-9])
    |		#    ...or
   2[0-4]\\d	#    start with 2, follow by 0-4 and end with any digit (2[0-4][0-9])
    |           #    ...or
   25[0-5]      #    start with 2, follow by 5 and ends with 0-5 (25[0-5])
 )		#  end of group #2
  \.            #  follow by a dot "."
....            # repeat with 3 times (3x)
$		#end of the line

Whole combination means, digit from 0 to 255 and follow by a dot “.”, repeat 4 time and ending with no dot “.” Valid IP address format is “0-255.0-255.0-255.0-255”

时间: 2024-12-28 21:17:40

Java Regex match IP address的相关文章

获取IP Address

public string GetUserIp() { var visitorsIpAddr = string.Empty; if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null) { visitorsIpAddr = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FOR

java读取真IP数据库QQwry.dat的源代码(转载)

原帖:http://blog.csdn.net/swazn_yj/article/details/1611020 一.IPEntry.java /** ** 一条IP范围记录,不仅包括国家和区域,也包括起始IP和结束IP *  *  * @author swallow */public class IPEntry {    public String beginIp;    public String endIp;    public String country;    public Stri

java 求主机IP地址及其相对应的子网掩码

不多说,直接上代码. package com.ckw.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.Map.Entry; public class TestOfSubNetMask { Map<String, String> map = new HashMap<St

How do I use a host name to look up an IP address?

The InetAddress class can be used to perform Domain Name Server (DNS) lookups. For example, you can call the static InetAddress.getByName("www.teamcakes.com") to retrieve an InetAddress object for 'www.teamcakes.com'. This object would contain t

.net正则表达式大全(.net 的 System.Text.RegularExpressions.Regex.Match()方法使用)

正则表达式的本质是使用一系列特殊字符模式,来表示某一类字符串.正则表达式无疑是处理文本最有力的工具,而.NET的System.dll类库提供的System.Text.RegularExpressions.Regex类实现了验证正则表达式的方法.Regex 类表示不可变(只读)的正则表达式.它还包含各种静态方法,允许在不显式创建其他类的实例的情况下使用其他正则表达式类. 正则表达式的字符代表的说明: Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN

java获取客户端IP地址和MAC地址

最近项目中要获得客户端的mac地址.服务端是移植的centos系统,arm架构的盒子.客户端是手机和移动设备.(其它场景应该也是类似的) 首先要获得ip地址: 根据客户端的http请求,利用request.getRemoteAddr()获取客户端Ip地址.在局域网内getRemoteAddr()和getRemoteHost()获得的结果相同.request.getRemoteAddr()是获得客户端的ip地址 .getRemoteHost()是获得客户端的主机名 .在有些场景中,可能有Squid

Java如何从IP地址查找主机名?

在Java编程中,如何从IP地址查询出主机名? 以下示例显示了如何通过net.InetAddress类的InetAddress.getByName()方法将指定的IP地址查到主机名称. package com.yiibai; import java.net.InetAddress; public class HostSpecificByIP { public static void main(String[] argv) throws Exception { InetAddress addr =

LeetCode 1108. Defanging an IP Address

原题链接在这里:https://leetcode.com/problems/defanging-an-ip-address/ 题目: Given a valid (IPv4) IP address, return a defanged version of that IP address. A defanged IP address replaces every period "." with "[.]". Example 1: Input: address = &

Java获取访问者Ip并限制Ip访问页面

原文链接:https://www.zjhuiwan.cn/info/20200330/4006602464505049.html 最近遇到一个需求,一个只能内网访问的网站,需要限制ip访问.就是网站内的部分文章只有白名单内的ip才能打开.因为是静态化的网站,所有文章都是静态html页面.所以首先想到的就是直接js获取访问者ip然后再判断是否在白名单内,不在白名单内就到没有权限页面. 但是JS获取内网Ip还是比较麻烦的,查到几个方法最后试了都不行. 记录下查到的几个方法和最后实现的方法. JS获取