正则表达式中的基本正则规则详解02
正则表达式中的基本正则规则详解01 环视锚点对位置的判断不够灵活所以引出环视 作用:应用子表达式对位置进行判断 形式: (?=...) (?!...) (?<=...) (?<!...) 例子1: import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pa17 {
public static void main(String[] args) {
String[] strings = new String[] { "Jeff","Jeffrey","Jefferson"};
String[] regexes = new String[] { "Jeff","Jeff(?=rey)","Jeff(?!rey)"};
for (String regex : regexes) {
for (String str : strings) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if(m.find()) {
System.out.println(""" + str
+ "" can be matched with regex "" + regex
+ """);
}
else {
System.out.println(""" + str
+ "" can not be matched with regex "" + regex
+ """);
}
}
System.out.println("");
}
}
}
运行结果: "Jeff" can be matched with regex "Jeff"
"Jeffrey" can be matched with regex "Jeff"
"Jefferson" can be matched with regex "Jeff"
"Jeff" can not be matched with regex "Jeff(?=rey)"
"Jeffrey" can be matched with regex "Jeff(?=rey)"
"Jefferson" can not be matched with regex "Jeff(?=rey)"
"Jeff" can be matched with regex "Jeff(?!rey)"
"Jeffrey" can not be matched with regex "Jeff(?!rey)"
"Jefferson" can be matched with regex "Jeff(?!rey)"
例子2: import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pa18 {
public static void main(String[] args) {
String[] strings = new String[] {"see","bee","tee"};
String[] regexes = new String[] { "(?<=s)ee","(?<!s)ee"};
for (String regex : regexes) {
for (String str : strings) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if(m.find()) {
System.out.println(""" + str
+ "" can be matched with regex "" + regex
+ """);
}
else {
System.out.println(""" + str
+ "" can not be matched with regex "" + regex
+ """);
}
}
System.out.println("");
}
}
}
运行结果: "see" can be matched with regex "(?<=s)ee"
"bee" can not be matched with regex "(?<=s)ee"
"tee" can not be matched with regex "(?<=s)ee"
"see" can not be matched with regex "(?<!s)ee"
"bee" can be matched with regex "(?<!s)ee"
"tee" can be matched with regex "(?<!s)ee"
环视的注意事项
例子: import java.util.regex.*;
public class pa22{
public static void main(String args[]){
String sta="see";
String regex="(?<=s)ee";
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(sta);
while(matcher.find()){
System.out.println(matcher.group());
}
}
}
运行结果: ee 逆序环视结构对子表达式存在限制
环视应用实例
仔细总结就会知道这个规律: public class pa19{
public static void main(String args[]){
String sta="123456789";
String regex="(?<=d)(?=(?:d{3})+(?!d))";
System.out.println("["+sta.replaceAll(regex,",")+"]");
}
}
运行结果: [123,456,789]
不知道大家还记得我以前解释的那两个锚点不??//b和//B,在这里我就要用b给大家解释了。b和B的用法及区别及注意事项 b和B是分界符,^,$也是分界符,而我们这里说的环视的四个表达式也是分界符,(?=…)肯定顺序环视,(?!…)否定顺序环视,(?<=…)逆序肯定环视,(?< !…)逆序否定环视。它们四个是匹配子表达式的右边或者左边的那个边界。就像是b匹配单词和符号之间的边界一样。 再结合下面这个例子来解释: public class pa20{
public static void main(String args[]){
String sta="123456789";
String regex="(?=d)";
System.out.println("####################### 1 ##########################");
System.out.println("1是想让你知道,它是否能够切割字符,并且你要仔细斟酌它是从哪儿里切割的!!!");
String arrays[]=sta.split(regex);
for(String i:arrays){
System.out.println("["+i+"]");
}
System.out.println("####################### 2 ##########################");
System.out.println("2是想让你知道这个答案,它是从哪儿被切割的!!!");
System.out.println(sta.replaceAll(regex,"));
}
}
运行结果: ####################### 1 ########################## 1是想让你知道,它是否能够切割字符,并且你要仔细斟酌它是从哪儿里切割的!!! [1] [2] [3] [4] [5] [6] [7] [8] [9] ####################### 2 ##########################
2是想让你知道这个答案,它是从哪儿被切割的!!!,1,2,3,4,5,6,7,8,9
是否这个切割的部位让你出乎意料呢??? 所以那个数字格式整理的例子也就迎刃而解了。 那么想不想看看”b”的匹配规则呢?? 例子: public class pa21{
public static void main(String args[]){
String sta="1-2-3-4-5-6";
String regex="b";
System.out.println(sta.replaceAll(regex,"|"));
}
}
运行结果: |1|-|2|-|3|-|4|-|5|-|6|
b在每个单词和符号的分界地方都添加了”|”号(因为是replaceAll),也包括字符开头和结尾!!!字符串的开头和结尾当然也在b的规定范围中。 这也可以明显看出,b和环视一样,都是匹配边界的。 匹配模式
形式:
1.I:不区分大小写 首先来看这个例子: import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pa23 {
public static void main(String[] args) {
String str = "abc";
String regex = "ABC";
Pattern p = Pattern.compile(regex);//
Matcher m = p.matcher(str);
if(m.find()) {
System.out.println(""" + str
+ "" can be matched with regex "" + regex
+ """);
} else {
System.out.println(""" + str
+ "" can not be matched with regex "" + regex
+ """);
}
}
}
运行结果: "abc" can not be matched with regex "ABC"
然后我们来加上匹配模式试试: import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pa23 {
public static void main(String[] args) {
String str = "abc";
String regex = "ABC";
Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);//这里加了个CASE_INSENSITIVE
Matcher m = p.matcher(str);
if(m.find()) {
System.out.println(""" + str
+ "" can be matched with regex "" + regex
+ """);
} else {
System.out.println(""" + str
+ "" can not be matched with regex "" + regex
+ """);
}
}
}
运行结果: "abc" can be matched with regex "ABC"
2.S: 单行模式
例子1: import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pa24 {
public static void main(String[] args) {
String str = "<a href=www.itcast.net>nITCASTn</a>";
String regex = "<a href=([^>]+)>.*</a>";
Pattern p = Pattern.compile(regex);//
Matcher m = p.matcher(str);
if(m.find()) {
System.out.println(""" + str
+ "" can be matched with regex "" + regex
+ """);
} else {
System.out.println(""" + str
+ "" can not be matched with regex "" + regex
+ """);
}
}
}
运行结果: "<a href=www.itcast.net>
ITCAST
</a>" can not be matched with regex "<a href=([^>]+)>.*</a>"
所以,在普通模式下是不能够匹配换行的。 例子2: import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pa24 {
public static void main(String[] args) {
String str = "<a href=www.itcast.net>nITCASTn</a>";
String regex = "<a href=([^>]+)>.*</a>";
Pattern p = Pattern.compile(regex,Pattern.DOTALL);//加了个DOTALL匹配模式,就能够匹配换行和回车符号了。
Matcher m = p.matcher(str);
if(m.find()) {
System.out.println(""" + str
+ "" can be matched with regex "" + regex
+ """);
} else {
System.out.println(""" + str
+ "" can not be matched with regex "" + regex
+ """);
}
}
}
运行结果: "<a href=www.itcast.net>
ITCAST
</a>" can be matched with regex "<a href=([^>]+)>.*</a>"
这下就匹配了所有字符,包括换行和回车符号。 3.M: 多行模式
例子1: import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pa25 {
public static void main(String[] args) {
String str = "<a href=www.itcast.net>nITCASTn</a>";
String regex = "^ITCAST$";
Pattern p = Pattern.compile(regex);//
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(""" + str + "" can be matched with regex ""
+ regex + """);
} else {
System.out.println(""" + str
+ "" can not be matched with regex "" + regex + """);
}
}
}
运行结果: "<a href=www.itcast.net> ITCAST </a>" can not be matched with regex "^ITCAST$"
很明显,在默认情况下, “^”和 “$”是匹配整个字符串的开始和结尾的。 那么在修改了匹配模式之后呢??是否匹配能够有所改变!!! 例子2: import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pa25 {
public static void main(String[] args) {
String str = "<a href=www.itcast.net>nITCASTn</a>";
String regex = "^ITCAST$";
Pattern p = Pattern.compile(regex,Pattern.MULTILINE);//
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(""" + str + "" can be matched with regex ""
+ regex + """);
} else {
System.out.println(""" + str
+ "" can not be matched with regex "" + regex + """);
}
}
}
运行结果: "<a href=www.itcast.net>
ITCAST
</a>" can be matched with regex "^ITCAST$"
所以,修改匹配模式,用来匹配整段字符串的逻辑行的开始和结尾是有效的。 4.X: 注释模式
例子: import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pa27 {
public static void main(String[] args) {
String str = "webmaster@itcast.net";
String regex = "w+ #usernamen" + "@" + "[w.]+ #hostname";
//相当于"w+@[w.]+"
Pattern p = Pattern.compile(regex,Pattern.COMMENTS);//
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(""" + str + "" can be matched with regex ""
+ regex + """);
} else {
System.out.println(""" + str
+ "" can not be matched with regex "" + regex + """);
}
}
}
运行结果: "webmaster@itcast.net" can be matched with regex "w+ #username @[w.]+ #hostname"
模式的混合
例子: import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pa28 {
public static void main(String[] args) {
String str = "<A HREF=www.itcast.net>nITCASTn</A>";
String regex = "<a href=([^>]+)>.*</a>";
Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(""" + str + "" can be matched with regex ""
+ regex + """);
} else {
System.out.println(""" + str
+ "" can not be matched with regex "" + regex + """);
}
}
}
运行结果: "<A HREF=www.itcast.net>
ITCAST
</A>" can be matched with regex "<a href=([^>]+)>.*</a>"
然后,这样就能同时使用多个模式。 模式的作用范围
例子1: public class pa30{
public static void main(String args[]){
String sta="abc";
String regex="(?i)ABC";//没有写(?-i),并且(?i)处于正则的开始,所以就表示整个正则表达式都是忽略大小写的。
if(sta.matches(regex)){
System.out.println("匹配成功!!!");
}
}
}
运行结果: 匹配成功!!! 然后我们来看看怎么停用作用范围的例子: public class pa31{
public static void main(String args[]){
String sta="abc";
String regex="(?i)AB(?-i)C";
if(sta.matches(regex)){
System.out.println("匹配成功!!!");
}else{
System.out.println("匹配失败!!!");
}
}
}
运行结果: 匹配失败!!! 作用范围的多模式匹配 import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pa32 {
public static void main(String[] args) {
String str = "<A Href=www.itcast.net>nITCASTn</A>";
String regex = "(?is)<a href=([^>]+)>.*</a>";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(""" + str + "" can be matched with regex ""
+ regex + """);
} else {
System.out.println(""" + str
+ "" can not be matched with regex "" + regex + """);
}
}
}
运行结果: "<A Href=www.itcast.net>
ITCAST
</A>" can be matched with regex "(?is)<a href=([^>]+)>.*</a>"
模式的冲突
例子: import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pa33 {
public static void main(String[] args) {
String str = "abc";
String regex = "(?-i)ABC";
Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);//
Matcher m = p.matcher(str);
if(m.find()) {
System.out.println(""" + str
+ "" can be matched with regex "" + regex
+ """);
} else {
System.out.println(""" + str
+ "" can not be matched with regex "" + regex
+ """);
}
}
}
运行结果: "abc" can not be matched with regex "(?-i)ABC"
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |