一、正则表达式应用
package com.jun.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 正则表达式应用
*/
public class RegExp10 {
public static void main(String[] args) {
//验证是否是汉字
//String content ="老黄牛";
//String regStr ="^[\u0391-\uffe5]+$";
//验证邮政编码
String content = "123456";
//String regStr = "^[1-9]\\d{5}$";
//QQ号码验证
//String regStr ="^[1-9]\\d{4,9}$";
//手机号验证
String regStr ="^1[3|8]\\d{9}$";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
System.out.println("满足格式");
} else {
System.out.println("不满足格式");
}
}
}
二、url地址验证
package com.jun.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* url验证
*/
public class RegExp11 {
public static void main(String[] args) {
String content ="https://www.bilibili.com/video/BV1xQ4y1R7Mo?spm_id_from=333.851.b_7265636f6d6d656e64.4";
/**
* 1.确定url的开始部分https:// | http://
* 2.通过([\w-]+\.)+[\w-]+匹配www.bilibili.com
* 3.(\/[\w-?=&/%.#]*)?匹配video/BV1xQ4y1R7Mo?spm_id_from=333.851.b_7265636f6d6d656e64.4
*/
String regStr ="^((http|https)://)?([\\w-]+\\.)+[\\w-]+(\\/[\\w-?=&/%.#]*)?$";//[.]就表示匹配.的本身
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
System.out.println("满足格式");
} else {
System.out.println("不满足格式");
}
}
}