IP归属地在线查询平台 javaSE 优化序列化写法

 12.BUG优化

用户输入 需要严格校验,比如IP地址不正确,还有必要去查询吗?

12.1 技术问题

正则表达式

Pattern和Matcher

Pattern是正则表达式引擎

Matcher是匹配器

Matches : 全词匹配

Find : 任意位置

lookingAt : 从前往后匹配

package com;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class TestRegex_01 {

public static void main(String[] args) {

String regex = "\\d{11}";

String tel = "131131131121";

// 创建引擎

Pattern pattern = Pattern.compile(regex);

// 创建匹配器

Matcher matcher = pattern.matcher(tel);

// System.out.println(matcher.matches());

// System.out.println(matcher.find());

System.out.println(matcher.lookingAt());

tel = "我的电话是13113113111";

regex = "我的电话是(\\d{11})";

pattern = Pattern.compile(regex);

matcher = pattern.matcher(tel);

matcher.find();

System.out.println(matcher.group(1));

}

}

12.2 封装工具类

package com.tledu.zrz.util;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

 * 正则表达式工具类

 *

 * @author 天亮教育-帅气多汁你泽哥

 * @Date 2021年11月12日 上午9:37:50

 */

public class RegexUtil {

/**

 * 格式验证

 *

 * @param regex

 * @param input

 * @return

 */

public static boolean isValid(String regex, String input) {

Pattern pattern = Pattern.compile(regex);

// 创建匹配器

Matcher matcher = pattern.matcher(input);

return matcher.matches();

}

/**

 * 数据提取

 *

 * @param regex

 * @param input

 * @param groupIndex

 * @return

 */

public static String getMatchContent(String regex, String input,

int groupIndex) {

Pattern pattern = Pattern.compile(regex);

// 创建匹配器

Matcher matcher = pattern.matcher(input);

if (matcher.find()) {

return matcher.group(groupIndex);

}

return null;

}

public static String getMatchContent(String regex, String input) {

return getMatchContent(regex, input, 0);

}

}

12.3 测试工具类

package com;

import com.tledu.zrz.util.RegexUtil;

public class TestRegex_02 {

public static void main(String[] args) {

String regex = "\\d{11}";

String tel = "13113113111";

System.out.println(RegexUtil.isValid(regex, tel));

tel = "我的电话是13113113111";

regex = "我的电话是(\\d{11})";

System.out.println(RegexUtil.getMatchContent(regex, tel,1));

}

}

 12.4业务问题

package com.tledu.zrz.util;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

 * 正则表达式工具类

 *

 * @author 天亮教育-帅气多汁你泽哥

 * @Date 2021年11月12日 上午9:37:50

 */

public class RegexUtil {

private static String regexIP = "((25[0-5]|2[0-4]\\d|[1]{1}\\d{1}\\d{1}|[1-9]{1}\\d{1}|\\d{1})($|(?!\\.$)\\.)){4}";

/**

 * 校验IP

 * @param ip

 * @return

 */

public static boolean isValidIP(String ip) {

return isValid(regexIP, ip);

}

/**

 * 格式验证

 *

 * @param regex

 * @param input

 * @return

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值