过滤ip的正则表达式
private static final String IP_REGEX = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
/**
* 根据输入的字符串,过滤出ip地址
* @param str
* @return 返回过滤出来的ip地址,如果没有匹配到,则返回null
*/
public static String getIpByString(String str) {
Pattern pattern = Pattern.compile(IP_REGEX);
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
return matcher.group();
} else {
return null;
}
}
/** 判断是否是一个IP */ //127.1.1.1 127.0.0.1 192..168..1 192...168..1 192...168..1 255...255....255 0.....256....1234......888......999.....876 0...256....1234......888......999.....876 0.....256....1234......888......999.....8761234567890987654321123456789098765432109876543211234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+:<>?/.,;'[]{}`
\r\"
public static boolean isIpAddress(String ipAddress) {
// 定义正则表达式 \\表示 \ 符号要用 \\ 表示转义; . 表示任意字符;* 表示零次或多次匹配前面的字符或子表达式。 + 表示一次或多次匹配前面的字符或子表达式。 ? 表示零次或一次匹配前面的字符或子表达式。 {n,m}最少匹配n次且最多匹配m 次。 \s 匹 配任何不可见字 符,包 括 空 格、制表 符、 换 页 等。 \S 匹 配任何可见字 符。 \w 匹 配任何字 母数字及下划 线 [a-zA-Z_0-9], 例如 'a'、'A'、'3'、'$'及 '_‘。 \W 匹 配任何非单词字 类 [^a-zA-Z_0-9], 例如 '%', '@', '#', '[','('及 '/‘。 ^ 子串开始 (^abc$) $ 子串定界 (abc$) | 或者 () ()中间写正则 [] []中间写正则 [^] []中间写正则,不能准备的都通过 & 所有特性都必 需具 备执行 \b 单词界定 (<html>\b</html>) d 数值([+\-]?\d+) D 数值([+\-]?\D+) s 任意一个white space character ([
\x0B\f\r]) S 任意一个non white space character ([^
\x0B\f\r]) w word characters([a - zA - Z_ 0 - 9]) W non word characters([^ a - zA - Z_ 0 - 9])
String regex = "((?:(?:25[0-5]|2[
创作工场
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!