应用场景
Java Pattern 与 Macher 类常用来通过正则表达式进行数据格式的校验
常用方法
Pattern
方法 | 简介 |
---|
boolean matches(String regex, CharSequence input) | 将输入字符串 input 与正则表达式 regex 进行匹配,返回匹配结果 |
Pattern compile(String regex) | 传入正则表达式,获得一个 Pattern 对象,可以多次使用 |
Matcher matcher(CharSequence input) | 传入待匹配的字符串,获得匹配结果对象 Matcher |
Matcher
方法 | 简介 |
---|
boolean find() | 返回正则匹配的结果 |
String group() | 返回被匹配字符串中满足正则匹配的字符串,若存在多个可配合 find() 循环取值 |
int start() | 获得匹配字符串的起始索引 |
int end() | 获得匹配字符串的终止索引 |
正则表达式元字符
在 Java 中使用时注意使用转义符,对应的测试和结果见 demo
字符 | 解释 |
---|
无元字符 | 字符串与正则表达式完全一致时返回 true |
\d | 表示一个数字 |
\D | 表示一个非数字 |
\w | 表示一个大小写字母、数字、下划线 |
\W | 表示一个非大小写字母、数字、下划线 |
\s | 表示空格或者制表符 |
\S | 表示一个非空格或制表符 |
. | 表示除”\n“和”\r“之外的任意字符 |
| | 表示逻辑关系”或“ |
[] | 表示匹配其中的任意一个字符 |
[^] | 表示不与其中的任何一个字符匹配 |
[X-X] | 表示匹配 X-X 之间的任意字符 |
[^X-X] | 表示不与范围内的任意一个字符匹配 |
\NUM | NUM 为数字,表示匹配第 NUM 个括号的结果 |
? | 表示前面的子表达式出现一次或零次 |
+ | 表示前面的子表达式出现一次或多次 |
{n} | 匹配前面确定的子表达式 n 次 |
{n,N} | 匹配前面确定的子表达式 n 到 N 次 |
* | 匹配前面的子表达式任意次 |
PatternDemo
package JavaPattern;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternDemo {
public static void main(String[] args) {
String strRegex = "ab*c";
boolean result = Pattern.matches(strRegex,"ac");
System.out.println(result);
String str = "电话1:13812341234;电话2:18812344321;电话3:15843214321";
String phoneRegex = "((13\\d)|(15\\d))\\d{8}";
Matcher matcher1 = Pattern.compile(phoneRegex).matcher(str);
while (matcher1.find()) {
System.out.println(matcher1.group() + "--开始index:" + matcher1.start()+ "--结束index:" + matcher1.end());
}
String regex = "abc";
System.out.println(check(regex, "abc123"));
System.out.println(check(regex, "abcabc"));
System.out.println(check(regex, "abc"));
regex = "abc(\\d)def";
System.out.println(check(regex, "abc123def"));
System.out.println(check(regex, "abc3def"));
regex = "abc(\\D)def";
System.out.println(check(regex, "abc3def"));
System.out.println(check(regex, "abcXdef"));
regex = "ab(\\w)de";
System.out.println(check(regex, "abCde"));
System.out.println(check(regex, "abcde"));
System.out.println(check(regex, "ab3de"));
System.out.println(check(regex, "ab_de"));
regex = "ab(\\W)de";
System.out.println(check(regex, "ab_de"));
System.out.println(check(regex, "ab@de"));
regex = "ab(\\s)de";
System.out.println(check(regex, "abcde"));
System.out.println(check(regex, "ab de"));
regex = "ab(\\S)de";
System.out.println(check(regex, "abcde"));
System.out.println(check(regex, "ab de"));
regex = "...";
System.out.println(check(regex, "123"));
System.out.println(check(regex, "abc"));
System.out.println(check(regex, "_*@"));
regex = "ab(\\d|\\s)de";
System.out.println(check(regex, "abcde"));
System.out.println(check(regex, "ab3de"));
System.out.println(check(regex, "ab de"));
regex = "ab[3c]de";
System.out.println(check(regex, "abXde"));
System.out.println(check(regex, "ab3de"));
System.out.println(check(regex, "abcde"));
regex = "ab[^3c]de";
System.out.println(check(regex, "abXde"));
System.out.println(check(regex, "ab3de"));
System.out.println(check(regex, "abcde"));
regex = "ab[a-z]de";
System.out.println(check(regex, "abhde"));
System.out.println(check(regex, "abHde"));
regex = "ab[^0-9]de";
System.out.println(check(regex, "ab9de"));
System.out.println(check(regex, "ab@de"));
regex = "(a)1b2c3d4e(5):\\1\\2";
System.out.println(check(regex, "a1b2c3d4e5:b4"));
System.out.println(check(regex, "a1b2c3d4e5:a5"));
regex = "(\\d)?abc";
System.out.println(check(regex, "123abc"));
System.out.println(check(regex, "abc"));
System.out.println(check(regex, "0abc"));
regex = "(\\d)+abc";
System.out.println(check(regex, "123abc"));
System.out.println(check(regex, "abc"));
System.out.println(check(regex, "0abc"));
regex = "(\\d){3}abc";
System.out.println(check(regex, "123abc"));
System.out.println(check(regex, "123123123abc"));
regex = "(\\d){1,3}abc";
System.out.println(check(regex, "123abc"));
System.out.println(check(regex, "123123abc"));
regex = "(\\d)*abc";
System.out.println(check(regex, "123abc"));
System.out.println(check(regex, "123123123abc"));
System.out.println(check(regex, "abc"));
}
private static boolean check(String regex, String input) {
return Pattern.matches(regex, input);
}
}