TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
单词分界符注意事项:
表示单词分界符,要求一侧是单词字符,另一侧是非单词字符
单词字符通常指的是英文字符、数字字符,对中文不适用
非单词字符通常指的是各种标点符号和空白字符
例子:
public class GeneralThree {
public static void main(String[] args) {
String[] strings = {"This sentence contains word cat",
"This sentence contains word "cat"",
"This sentence contains word vacation",
"This sentence contains word "cate"",
"句子里包含的英文单词cat0在这里",
"句子里包含的英文单词cat在这里"
};
String regex = "\bcat\b";
for(String str:strings){
System.out.println("处理句子:"+str);
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if(m.find()){
System.out.println("找到 cat !");
}else{
System.out.println("没有找到 cat !");
}
}
}
}
运行结果:
处理句子:This sentence contains word cat
找到 cat !
处理句子:This sentence contains word "cat"
找到 cat !
处理句子:This sentence contains word vacation
没有找到 cat !
处理句子:This sentence contains word "cate"
没有找到 cat !
处理句子:句子里包含的英文单词cat0在这里
没有找到 cat !
处理句子:句子里包含的英文单词cat在这里
没有找到 cat !
可见:
第三个、不能区分t0英文字符和数字字符之间的位置。
第四个、不能区分t在英文字符和中文字符之间的位置。
锚点:
^ 匹配一行的开头(有可能变化)
$ 匹配一行的结尾(有可能变化)
A 匹配整个字符串的开头
匹配整个字符串的末尾
^和$在不同的匹配模式下,可能有所变化,匹配模式的概念将在下一节课介绍。
默认情况下:
^等于A 用来匹配整个字符串的开头
$等于 用来匹配整个字符串的结尾
如果匹配某一个逻辑行的开头,和某一个逻辑行的结尾?
这个需要更改匹配模式。
看看这四个锚点的基本应用。
例子:
public class GeneralFour {
public static void main(String[] args) {
String[] strings = {"start"," start "," end ","end"};
String[] regexs = {"^start","\Astart","end$","end\Z"};
for(String str:strings){
for(String regex:regexs){
System.out.println("处理句子:"+str);
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if(m.find()){
System.out.println(str+"能够匹配正则:"+regex);
}else{
System.out.println(str+"不能够匹配正则:"+regex);
}
}
}
}
}
运行结果:
处理句子:start
start能够匹配正则:^start
处理句子:start
start能够匹配正则:Astart
处理句子:start
start不能够匹配正则:end$
处理句子:start
start不能够匹配正则:end
处理句子: start
start 不能够匹配正则:^start
处理句子: start
start 不能够匹配正则:Astart
处理句子: start
start 不能够匹配正则:end$
处理句子: start
start 不能够匹配正则:end
处理句子: end
end 不能够匹配正则:^start
处理句子: end
end 不能够匹配正则:Astart
处理句子: end
end 不能够匹配正则:end$
处理句子: end
end 不能够匹配正则:end
处理句子:end
end不能够匹配正则:^start
处理句子:end
end不能够匹配正则:Astart
处理句子:end
end能够匹配正则:end$
处理句子:end
end能够匹配正则:end
下面一个功能:
环视
锚点对位置的判断不够灵活
作用:应用子表达式对位置进行判断
形式:
(?=...)
(?!...)
(?<...)
(?<!...)
形式
名称
匹配意义
(?=...)
肯定顺序环视
右侧文本能由子表达式匹配
(?!...)
否定顺序环视
右侧文本不能由子表达式匹配
(?<...)
肯定逆序环视
左侧文本能由子表达式匹配
(?<!...)
否定逆序环视
左侧文本不能由子表达式匹配
首先看看顺序环视结构的例子:
public class GeneralFive {
public static void main(String[] args) {
String[] strings = new String[]{"Jeff","Jeffrey","Jefferson"};
String[] regexs = new String[]{"Jeff","Jeff(?=rey)","Jeff(?!rey)"};
for(String regex:regexs){
for(String str:strings){
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if(m.find()){
System.out.println(""" + str +"" 能够匹配正则:"+regex);
}else{
System.out.println(""" + str +"" 不能够匹配正则:"+regex);
}
}
System.out.println("");
}
}
}
运行结果:
"Jeff" 能够匹配正则:Jeff
"Jeffrey" 能够匹配正则:Jeff
"Jefferson" 能够匹配正则:Jeff
"Jeff" 不能够匹配正则:Jeff(?=rey)
"Jeffrey" 能够匹配正则:Jeff(?=rey)
"Jefferson" 不能够匹配正则:Jeff(?=rey)
"Jeff" 能够匹配正则:Jeff(?!rey)
"Jeffrey" 不能够匹配正则:Jeff(?!rey)
"Jefferson" 能够匹配正则:Jeff(?!rey)
本文出自 “leolee” 博客 |
|