1.限定符
2.实例学习
1){n}
n 是一个非负整数。匹配确定的 n 次。
例子:
public static void main(String[] args) {
String str="liife is a fuck _movie";
//获得一个正则表达式对象
Pattern p = Pattern.compile("li{2}fe.*");
//使用正则表达式对象处理指定字符串,并获得结果对象
Matcher m = p.matcher(str);
//从正则表达式结果对象中获得信息
while (true){
if(m.find()) {
System.out.print(m.group());
}else break;
}
}
2){n,}
n 是一个非负整数。至少匹配n 次。
例子:
public static void main(String[] args) {
String str="liiife is a fuck _movie";
//获得一个正则表达式对象
Pattern p = Pattern.compile("li{2,}fe.*");
//使用正则表达式对象处理指定字符串,并获得结果对象
Matcher m = p.matcher(str);
//从正则表达式结果对象中获得信息
while (true){
if(m.find()) {
System.out.print(m.group());
}else break;
}
}
3){n,m}
m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次
例子:
public static void main(String[] args) {
String str="liife is a fuck _movie";
//获得一个正则表达式对象
Pattern p = Pattern.compile("li{2,3}fe.*");
//使用正则表达式对象处理指定字符串,并获得结果对象
Matcher m = p.matcher(str);
//从正则表达式结果对象中获得信息
while (true){
if(m.find()) {
System.out.print(m.group());
}else break;
}
}