java 正则 工具_Java代码使用正则验证和常用工具方法

1.正则验证邮箱

public static boolean checkEmail(String email){

boolean flag = false;

try{

String check = "^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$";

Pattern regex = Pattern.compile(check);

Matcher matcher = regex.matcher(email);

flag = matcher.matches();

}catch(Exception e){

flag = false;

}

return flag;

}

2.验证昵称是否合法

/**

* 验证用户名,支持中英文(包括全角字符)、数字、下划线和减号 (全角及汉字算两位),长度为4-20位,中文按二位计数

* @return

*/

public static boolean validateUserName(String userName) {

String validateStr = "^[\\w\\--_[0-9]\u4e00-\u9fa5\uFF21-\uFF3A\uFF41-\uFF5A]+$";

boolean rs = false;

rs = matcher(validateStr, userName);

if (rs) {

int strLenth = getStrLength(userName);

if (strLenth < 4 || strLenth > 15) {

rs = false;

}

}

return rs;

}

/**

* 获取字符串的长度,对双字符(包括汉字)按两位计数

* @return

*/

public static int getStrLength(String value) {

int valueLength = 0;

String chinese = "[\u0391-\uFFE5]";

for (int i = 0; i < value.length(); i++) {

String temp = value.substring(i, i + 1);

if (temp.matches(chinese)) {

valueLength += 2;

} else {

valueLength += 1;

}

}

return valueLength;

}

private static boolean matcher(String reg, String string) {

boolean tem = false;

Pattern pattern = Pattern.compile(reg);

Matcher matcher = pattern.matcher(string);

tem = matcher.matches();

return tem;

}

3.判断是否是数字

public static boolean isInteger(String value) {

Pattern p = Pattern.compile("^[0-9]{11}$");

Matcher m = p.matcher(value);

boolean isOK = m.find();

return isOK;

}

4.创建随机数

public static int buildRandom(int length) {

int num = 1;

double random = Math.random();

if (random < 0.1) {

random = random + 0.1;

}

for (int i = 0; i < length; i++) {

num = num * 10;

}

return (int) ((random * num));

}

5.获取机型,浏览器类型

public static String getTypeas(HttpServletRequest request) {

String typeas= request.getHeader("User-Agent");

if (typeas.equals("")||typeas==null) {

typeas="nothing";

}

return typeas;

}

5.获取IP地址

public static String getIp(HttpServletRequest request) {

String ip = request.getHeader("x-forwarded-for");

if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip = request.getHeader("Proxy-Client-IP");

}

if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip = request.getHeader("WL-Proxy-Client-IP");

}

if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip = request.getRemoteAddr();

}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

ip="unknown";

}

return ip;

}

6.判断数字是否在数组内

public static boolean isIn(Integer substring, Integer[] source) {

if (source == null || source.length == 0) {

return false;

}

for (int i = 0; i < source.length; i++) {

Integer aSource = source[i];

if (aSource.equals(substring)) {

return true;

}

}

return false;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值