一.方法说明:
find()方法是部分匹配,在部分匹配时和完全匹配时返回true,匹配不上返回false。如果该匹配的串有组还可以使用group()函数,来获取串中的各个group
matches()是全部匹配,它只有在完全匹配时返回true,匹配不上和部分匹配都返回false。e。如果要验证一个输入的数据是否为全部为数字类型或其他类型,一般要用matches()。
二.一般写法
Pattern pattern= Pattern.compile(".*?,(.*)");
Matcher matcher = pattern.matcher(result);
if (matcher.find()) {
return matcher.group(1);
}
三.详解:
matches
public static boolean matches(String regex, CharSequence input)
编译给定正则表达式并尝试将给定输入与其匹配。
调用此便捷方法的形式
Pattern.matches(regex, input);
Pattern.compile(regex).matcher(input).matches() ;
如果要多次使用一种模式,编译一次后重用此模式比每次都调用此方法效率更高。
参数:
regex - 要编译的表达式
input - 要匹配的字符序列
抛出:
PatternSyntaxException - 如果表达式的语法无效
find
public boolean find()尝试查找与该模式匹配的输入序列的下一个子序列。
此方法从匹配器区域的开头开始,每次调用find(),都会从上一次find()成功匹配的位置继续向后查找,直到到达字符串末尾。第一次执行find()将从字符串开始位置查找。如果第一次调用find()返回true而第二次调用返回false,那说明字符串中只有一处与所给的正则表达式匹配。
如果匹配成功,则可以通过 start、end 和 group 方法获取更多信息。
matcher.start() 返回匹配到的子字符串在字符串中的索引位置.
matcher.end()返回匹配到的子字符串的最后一个字符在字符串中的索引位置.
matcher.group()返回匹配到的子字符串
返回:
当且仅当输入序列的子序列匹配此匹配器的模式时才返回 true。
四.部分JAVA正则表达式实例
①字符匹配
Pattern p = Pattern.compile(expression); // 正则表达式
Matcher m = p.matcher(str); // 操作的字符串
boolean b = m.matches(); //返回是否匹配的结果
System.out.println(b);
Pattern p = Pattern.compile(expression); // 正则表达式
Matcher m = p.matcher(str); // 操作的字符串
boolean b = m. lookingAt (); //返回是否匹配的结果
System.out.println(b);
Pattern p = Pattern.compile(expression); // 正则表达式
Matcher m = p.matcher(str); // 操作的字符串
boolean b = m..find (); //返回是否匹配的结果
System.out.println(b);
②分割字符串
Pattern pattern = Pattern.compile(expression); //正则表达式
String[] strs = pattern.split(str); //操作字符串 得到返回的字符串数组
③替换字符串
Pattern p = Pattern.compile(expression); // 正则表达式
Matcher m = p.matcher(text); // 操作的字符串
String s = m.replaceAll(str); //替换后的字符串
④查找替换指定字符串
Pattern p = Pattern.compile(expression); // 正则表达式
Matcher m = p.matcher(text); // 操作的字符串
StringBuffer sb = new StringBuffer();
int i = 0;
while (m.find()) {
m.appendReplacement(sb, str);
i++; //字符串出现次数
}
m.appendTail(sb);//从截取点将后面的字符串接上
String s = sb.toString();
⑤查找输出字符串
Pattern p = Pattern.compile(expression); // 正则表达式
Matcher m = p.matcher(text); // 操作的字符串
while (m.find()) {
matcher.start() ;
matcher.end();
matcher.group(1);
}
五.group的说明
group(),group(int i),groupcount() 这三个方法的区别,需要理解捕获组的意思。捕获组也就是Pattern中用()分割的子模式
以示例说明:
Pattern p = Pattern.compile("(ca)(t)");
Matcher m = p.matcher("one cat,two cats in the yard");
System.out.println("该次查找获得匹配组的数量为:" + m.groupCount()); // 2
for (int i = 0; i <= m.groupCount(); i++) {
System.out.println("第" + i + "组的子串内容为:" + m.group(i));
}
输出:
该次查找获得匹配组的数量为:2
第0组的子串内容为:cat
第1组的子串内容为:ca
第2组的子串内容为:t
可以这样理解:
m.groupCount()表示Pattern.compile()中的正则表达式中的()的个数。
m.group(0),等效于 m.group() 。是指输入序列str匹配整个模式,因此为cat
m.group(1)表示匹配正则表达式中的第一个括号里的内容即可,因此为ca,注意,也是第一次的值
m.group(2)表示匹配正则表达式中的第二个括号里的内容即可,因此为t,注意,也是第一次的值
groupcount() 返回的是正则表达式的捕获分组的数量(捕获分组和非捕获分组是另外的知识点),groupcount() 的结果并不能说明匹配的结果。
group是针对正则表达式中里面的()来说的,group(0)就是指的满足正则表达式的整个串,group(1) 指的是指正则表达式中第一个括号里的东西,group(2)指的正则表达式中第二个括号里的东西
示例代码:
public class MailTest{
public static void main(String[] args) throws Exception{
String regEx = "count(\\d+)(df)(df)*";
String s = "count000dfsdffcount123dfdfdfdfaaaa1"; //有两个匹配
Pattern pat = Pattern.compile(regEx);
Matcher mat = pat.matcher(s);
if(mat.find()){
int groupCount = mat.groupCount();
System.out.println(groupCount);
for(int i = 0;i <= groupCount;i++) {
System.out.println(mat.group(i));
}
}
System.out.println();
if(mat.find()){
int groupCount = mat.groupCount();
System.out.println(groupCount);
for(int i = 0;i <= groupCount;i++) {
System.out.println(mat.group(i));
}
//count(\\d+)(df)(df)*-->count(\\d+)(df)(df)(df)(df)
//groupCount()仅是从正则表达式里面捕获分组的数量(实际可见的"()"的个数),所以下面group(4)和5报错
//System.out.println(mat.group(4));//好像看起来第二个匹配串中有4
//System.out.println(mat.group(5));//好像看起来第二个匹配串中有5
}
}
}
输出结果:
3 //正则表达式里面有三个括号count(\\d+)(df)(df)*,所以groupCount始终为3,groupCount仅仅是正则表达式里面的()
count000df //匹配正则表达式的整个窜
000 //正则表达式里面第一个括号(\\d+)
df //正则表达式里面第二个括号(df)
null
3
count123dfdfdfdf
123
df
df
如果正则表达式里面没有来分组的"()"号,那么groupCount就为0,例如:
public class MailTest{
public static void main(String[] args) throws Exception{
String regEx = "count\\d+";
String s = "count000dfsdffcount123dfdfdfdfaaaa1"; //有两个匹配
Pattern pat = Pattern.compile(regEx);
Matcher mat = pat.matcher(s);
if(mat.find()){
int groupCount = mat.groupCount();
System.out.println(groupCount);
for(int i = 0;i <= groupCount;i++) {
System.out.println(mat.group(i));
}
}
System.out.println();
if(mat.find()){
int groupCount = mat.groupCount();
System.out.println(groupCount);
for(int i = 0;i <= groupCount;i++) {
System.out.println(mat.group(i));
}
}
}
}
输出结果:
0
count000
0
count123