正则表达式中的基本正则规则详解01
转载原文章链接:http://www.52php.cn/article/p-mumenqay-bqm.html 字符组
例子: import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li1 {
public static void main(String[] args) {
String string="4";
String rex="[0123456789]";
Pattern pattern=Pattern.compile(rex);
Matcher matcher=pattern.matcher(string);
System.out.println("/*********本例子用于判断数字是不是十进制*********/");
if(matcher.matches()){
System.out.println("tt是十进制数");
}else{
System.out.println("tt不是十进制数");
}
}
}
运行结果: /*********本例子用于判断数字是不是十进制*********/
是十进制数
连字符
可以测试下: import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li1 {
public static void main(String[] args) {
String string="4";
String rex="[0-9]";
Pattern pattern=Pattern.compile(rex);
Matcher matcher=pattern.matcher(string);
System.out.println("/*********本例子用于判断数字是不是十进制*********/");
if(matcher.matches()){
System.out.println("tt是十进制数");
}else{
System.out.println("tt不是十进制数");
}
}
}
运行结果: /*********本例子用于判断数字是不是十进制*********/
是十进制数
注意事项
排除型字符组
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li2 {
public static void main(String[] args) {
String [] strs={"1","2","a","A","n","t","r"," ","哈"};
String rex="[^123]";
Pattern pattern=Pattern.compile(rex);
for(String str:strs){
Matcher matcher=pattern.matcher(str);
System.out.println("字符串:"+str+"匹配结果"+"["+matcher.matches()+"]");
}
}
}
运行结果: 字符串:[1]匹配结果[false]
字符串:[2]匹配结果[false]
字符串:[a]匹配结果[true]
字符串:[A]匹配结果[true]
字符串:[
]匹配结果[true]
字符串:[ ]匹配结果[true]
字符串:[
]匹配结果[true]
字符串:[ ]匹配结果[true]
字符串:[哈]匹配结果[true]
所以排除型表示的意思就是排除当前的字符,然后满足其他的所有字符。 字符组简记法
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li3 {
public static void main(String[] args) {
String rex1="d";
String rex2="D";
String rex3="w";
String rex4="W";
String rex5="s";
String rex6="S";
String [] string=new String[]{"1","B","b","呵","!","!"};
System.out.println("/*************d****************/");
for(String str:string){
Pattern pattern=Pattern.compile(rex1);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"["+str+"]"+"匹配["+rex1+"]");
}else{
System.out.println("字符串:"+"["+str+"]"+"不匹配["+rex1+"]");
}
}
System.out.println("/*************D****************/");
for(String str:string){
Pattern pattern=Pattern.compile(rex2);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"["+str+"]"+"匹配["+rex2+"]");
}else{
System.out.println("字符串:"+"["+str+"]"+"不匹配["+rex2+"]");
}
}
System.out.println("/*************w****************/");
for(String str:string){
Pattern pattern=Pattern.compile(rex3);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"["+str+"]"+"匹配["+rex3+"]");
}else{
System.out.println("字符串:"+"["+str+"]"+"不匹配["+rex3+"]");
}
}
System.out.println("/*************W****************/");
for(String str:string){
Pattern pattern=Pattern.compile(rex4);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"["+str+"]"+"匹配["+rex4+"]");
}else{
System.out.println("字符串:"+"["+str+"]"+"不匹配["+rex4+"]");
}
}
System.out.println("/*************s****************/");
for(String str:string){
Pattern pattern=Pattern.compile(rex5);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"["+str+"]"+"匹配["+rex5+"]");
}else{
System.out.println("字符串:"+"["+str+"]"+"不匹配["+rex5+"]");
}
}
System.out.println("/*************S****************/");
for(String str:string){
Pattern pattern=Pattern.compile(rex6);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"["+str+"]"+"匹配["+rex6+"]");
}else{
System.out.println("字符串:"+"["+str+"]"+"不匹配["+rex6+"]");
}
}
}
}
运行结果: /*************d****************/
字符串:[1]匹配[d]
字符串:[2]匹配[d]
字符串:[A]不匹配[d]
字符串:[B]不匹配[d]
字符串:[a]不匹配[d]
字符串:[b]不匹配[d]
字符串:[
]不匹配[d]
字符串:[ ]不匹配[d]
字符串:[
]不匹配[d]
字符串:[ ]不匹配[d]
字符串:[呵]不匹配[d]
字符串:[!]不匹配[d]
字符串:[!]不匹配[d]
/*************D****************/
字符串:[1]不匹配[D]
字符串:[2]不匹配[D]
字符串:[A]匹配[D]
字符串:[B]匹配[D]
字符串:[a]匹配[D]
字符串:[b]匹配[D]
字符串:[
]匹配[D]
字符串:[ ]匹配[D]
字符串:[
]匹配[D]
字符串:[ ]匹配[D]
字符串:[呵]匹配[D]
字符串:[!]匹配[D]
字符串:[!]匹配[D]
/*************w****************/
字符串:[1]匹配[w]
字符串:[2]匹配[w]
字符串:[A]匹配[w]
字符串:[B]匹配[w]
字符串:[a]匹配[w]
字符串:[b]匹配[w]
字符串:[
]不匹配[w]
字符串:[ ]不匹配[w]
字符串:[
]不匹配[w]
字符串:[ ]不匹配[w]
字符串:[呵]不匹配[w]
字符串:[!]不匹配[w]
字符串:[!]不匹配[w]
/*************W****************/
字符串:[1]不匹配[W]
字符串:[2]不匹配[W]
字符串:[A]不匹配[W]
字符串:[B]不匹配[W]
字符串:[a]不匹配[W]
字符串:[b]不匹配[W]
字符串:[
]匹配[W]
字符串:[ ]匹配[W]
字符串:[
]匹配[W]
字符串:[ ]匹配[W]
字符串:[呵]匹配[W]
字符串:[!]匹配[W]
字符串:[!]匹配[W]
/*************s****************/
字符串:[1]不匹配[s]
字符串:[2]不匹配[s]
字符串:[A]不匹配[s]
字符串:[B]不匹配[s]
字符串:[a]不匹配[s]
字符串:[b]不匹配[s]
字符串:[
]匹配[s]
字符串:[ ]匹配[s]
字符串:[
]匹配[s]
字符串:[ ]匹配[s]
字符串:[呵]不匹配[s]
字符串:[!]不匹配[s]
字符串:[!]不匹配[s]
/*************S****************/
字符串:[1]匹配[S]
字符串:[2]匹配[S]
字符串:[A]匹配[S]
字符串:[B]匹配[S]
字符串:[a]匹配[S]
字符串:[b]匹配[S]
字符串:[
]不匹配[S]
字符串:[ ]不匹配[S]
字符串:[
]不匹配[S]
字符串:[ ]不匹配[S]
字符串:[呵]匹配[S]
字符串:[!]匹配[S]
字符串:[!]匹配[S]
特殊的简记法:点号
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li4 {
public static void main(String[] args) {
String rex1=".";
String rex2=".";
String rex3="[.]";
String [] string=new String[]{"1","!",".","。"};
System.out.println("/*************.****************/");
for(String str:string){
Pattern pattern=Pattern.compile(rex1);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex1+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex1+""");
}
}
System.out.println("/*************.****************/");
for(String str:string){
Pattern pattern=Pattern.compile(rex2);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex2+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex2+""");
}
}
System.out.println("/*************[.]****************/");
for(String str:string){
Pattern pattern=Pattern.compile(rex3);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex3+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex3+""");
}
}
}
}
运行结果: /*************.****************/
字符串:"1"匹配"."
字符串:"2"匹配"."
字符串:"A"匹配"."
字符串:"B"匹配"."
字符串:"a"匹配"."
字符串:"b"匹配"."
字符串:"
"不匹配"."
字符串:" "匹配"."
字符串:"
"不匹配"."
字符串:" "匹配"."
字符串:"呵"匹配"."
字符串:"!"匹配"."
字符串:"!"匹配"."
字符串:"."匹配"."
字符串:"。"匹配"."
/*************.****************/
字符串:"1"不匹配"."
字符串:"2"不匹配"."
字符串:"A"不匹配"."
字符串:"B"不匹配"."
字符串:"a"不匹配"."
字符串:"b"不匹配"."
字符串:"
"不匹配"."
字符串:" "不匹配"."
字符串:"
"不匹配"."
字符串:" "不匹配"."
字符串:"呵"不匹配"."
字符串:"!"不匹配"."
字符串:"!"不匹配"."
字符串:"."匹配"."
字符串:"。"不匹配"."
/*************[.]****************/
字符串:"1"不匹配"[.]"
字符串:"2"不匹配"[.]"
字符串:"A"不匹配"[.]"
字符串:"B"不匹配"[.]"
字符串:"a"不匹配"[.]"
字符串:"b"不匹配"[.]"
字符串:"
"不匹配"[.]"
字符串:" "不匹配"[.]"
字符串:"
"不匹配"[.]"
字符串:" "不匹配"[.]"
字符串:"呵"不匹配"[.]"
字符串:"!"不匹配"[.]"
字符串:"!"不匹配"[.]"
字符串:"."匹配"[.]"
字符串:"。"不匹配"[.]"
量词
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li5 {
public static void main(String[] args) {
String rex1="a*";
String rex2="a?";
String rex3="a+";
String [] string=new String[]{"","aa","aaa"};
System.out.println("/--------------a*-----------------/");
for(String str:string){
Pattern pattern=Pattern.compile(rex1);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex1+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex1+""");
}
}
System.out.println("/--------------a?-----------------/");
for(String str:string){
Pattern pattern=Pattern.compile(rex2);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex2+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex2+""");
}
}
System.out.println("/--------------a+-----------------/");
for(String str:string){
Pattern pattern=Pattern.compile(rex3);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex3+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex3+""");
}
}
}
}
运行结果: /--------------a*-----------------/
字符串:""匹配"a*"
字符串:"a"匹配"a*"
字符串:"aa"匹配"a*"
字符串:"aaa"匹配"a*"
/--------------a?-----------------/
字符串:""匹配"a?"
字符串:"a"匹配"a?"
字符串:"aa"不匹配"a?"
字符串:"aaa"不匹配"a?"
/--------------a+-----------------/
字符串:""不匹配"a+"
字符串:"a"匹配"a+"
字符串:"aa"匹配"a+"
字符串:"aaa"匹配"a+"
区间量词
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li6 {
public static void main(String[] args) {
String rex1="a{2,4}";
String rex2="a{2,}";
String rex3="a{3}";
String [] string=new String[]{"","aaa","aaaa","aaaaa","aaaaaa"};
System.out.println("/--------------a{2,4}-----------------/");
for(String str:string){
Pattern pattern=Pattern.compile(rex1);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex1+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex1+""");
}
}
System.out.println("/--------------a{2,}-----------------/");
for(String str:string){
Pattern pattern=Pattern.compile(rex2);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex2+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex2+""");
}
}
System.out.println("/--------------a{3}-----------------/");
for(String str:string){
Pattern pattern=Pattern.compile(rex3);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex3+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex3+""");
}
}
}
}
运行结果: /--------------a{2,4}-----------------/
字符串:""不匹配"a{2,4}"
字符串:"a"不匹配"a{2,4}"
字符串:"aa"匹配"a{2,4}"
字符串:"aaa"匹配"a{2,4}"
字符串:"aaaa"匹配"a{2,4}"
字符串:"aaaaa"不匹配"a{2,4}"
字符串:"aaaaaa"不匹配"a{2,4}"
/--------------a{2,}-----------------/
字符串:""不匹配"a{2,}"
字符串:"a"不匹配"a{2,}"
字符串:"aa"匹配"a{2,}"
字符串:"aaa"匹配"a{2,}"
字符串:"aaaa"匹配"a{2,}"
字符串:"aaaaa"匹配"a{2,}"
字符串:"aaaaaa"匹配"a{2,}"
/--------------a{3}-----------------/
字符串:""不匹配"a{3}"
字符串:"a"不匹配"a{3}"
字符串:"aa"不匹配"a{3}"
字符串:"aaa"匹配"a{3}"
字符串:"aaaa"不匹配"a{3}"
字符串:"aaaaa"不匹配"a{3}"
字符串:"aaaaaa"不匹配"a{3}"
量词的局限,括号的使用
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li7 {
public static void main(String[] args) {
String rex1="ac+";
String rex2="(ac)+";
String rex3="ac*";
String rex4="(ac)*";
String rex5="ac?";
String rex6="(ac)?";
String [] string=new String[]{"","ac","acc","accc","acacac","acac","c"};
System.out.println("/--------------ac+-----------------/");
for(String str:string){
Pattern pattern=Pattern.compile(rex1);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex1+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex1+""");
}
}
System.out.println("/--------------(ac)+-----------------/");
for(String str:string){
Pattern pattern=Pattern.compile(rex2);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex2+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex2+""");
}
}
System.out.println("/--------------ac*-----------------/");
for(String str:string){
Pattern pattern=Pattern.compile(rex3);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex3+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex3+""");
}
}
System.out.println("/--------------(ac)*-----------------/");
for(String str:string){
Pattern pattern=Pattern.compile(rex4);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex4+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex4+""");
}
}
System.out.println("/--------------ac?-----------------/");
for(String str:string){
Pattern pattern=Pattern.compile(rex5);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex5+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex5+""");
}
}
System.out.println("/--------------(ac)?-----------------/");
for(String str:string){
Pattern pattern=Pattern.compile(rex6);
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex6+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex6+""");
}
}
}
}
运行结果: /--------------ac+-----------------/
字符串:""不匹配"ac+"
字符串:"ac"匹配"ac+"
字符串:"acc"匹配"ac+"
字符串:"accc"匹配"ac+"
字符串:"acacac"不匹配"ac+"
字符串:"acac"不匹配"ac+"
字符串:"a"不匹配"ac+"
字符串:"c"不匹配"ac+"
/--------------(ac)+-----------------/
字符串:""不匹配"(ac)+"
字符串:"ac"匹配"(ac)+"
字符串:"acc"不匹配"(ac)+"
字符串:"accc"不匹配"(ac)+"
字符串:"acacac"匹配"(ac)+"
字符串:"acac"匹配"(ac)+"
字符串:"a"不匹配"(ac)+"
字符串:"c"不匹配"(ac)+"
/--------------ac*-----------------/
字符串:""不匹配"ac*"
字符串:"ac"匹配"ac*"
字符串:"acc"匹配"ac*"
字符串:"accc"匹配"ac*"
字符串:"acacac"不匹配"ac*"
字符串:"acac"不匹配"ac*"
字符串:"a"匹配"ac*"
字符串:"c"不匹配"ac*"
/--------------(ac)*-----------------/
字符串:""匹配"(ac)*"
字符串:"ac"匹配"(ac)*"
字符串:"acc"不匹配"(ac)*"
字符串:"accc"不匹配"(ac)*"
字符串:"acacac"匹配"(ac)*"
字符串:"acac"匹配"(ac)*"
字符串:"a"不匹配"(ac)*"
字符串:"c"不匹配"(ac)*"
/--------------ac?-----------------/
字符串:""不匹配"ac?"
字符串:"ac"匹配"ac?"
字符串:"acc"不匹配"ac?"
字符串:"accc"不匹配"ac?"
字符串:"acacac"不匹配"ac?"
字符串:"acac"不匹配"ac?"
字符串:"a"匹配"ac?"
字符串:"c"不匹配"ac?"
/--------------(ac)?-----------------/
字符串:""匹配"(ac)?"
字符串:"ac"匹配"(ac)?"
字符串:"acc"不匹配"(ac)?"
字符串:"accc"不匹配"(ac)?"
字符串:"acacac"不匹配"(ac)?"
字符串:"acac"不匹配"(ac)?"
字符串:"a"不匹配"(ac)?"
字符串:"c"不匹配"(ac)?"
括号的用途:多选结构
public class li8 {
public static void main(String[] args) {
String [] string=new String[]{"this","that","thit"};
String rex="th[ia][st]";
Pattern pattern=Pattern.compile(rex);
for(String str:string){
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex+""");
}
}
}
}
运行结果: 字符串:"this"匹配"th[ia][st]"
字符串:"that"匹配"th[ia][st]"
字符串:"thit"匹配"th[ia][st]"
而我们不想匹配错误的单词thit怎么办呢?? import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li8 {
public static void main(String[] args) {
String [] string=new String[]{"this","thit"};
String rex="this|that";//也可以写成th(is|at) 这样把公共提出来后可以提高正则匹配效率。
Pattern pattern=Pattern.compile(rex);
for(String str:string){
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex+""");
}
}
}
}
运行结果: 字符串:"this"匹配"this|that"
字符串:"that"匹配"this|that"
字符串:"thit"不匹配"this|that"
括号的用途:捕获分组
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li9 {
public static void main(String[] args) {
String email="webmaster@itcast.net";
String rex="(w+)@([w.]+)";
Pattern pattern=Pattern.compile(rex);
Matcher matcher=pattern.matcher(email);
if(matcher.find()){
System.out.println("email add is:t"+matcher.group(0));//默认打印的是整个匹配的结果
System.out.println("username is:t"+matcher.group(1));//打印从左到右第一个括号内的结果
System.out.println("hostname is:t"+matcher.group(2));//打印从左到右第二个括号内的结果
}
}
}
运行结果: email add is: webmaster@itcast.net
username is: webmaster
hostname is: itcast.net
需要强调的是:括号的先后顺序按照左括号的出现顺序编号,编号从1开始。编号0为整个正则表达式的匹配结果!! 捕获分组的注意事项
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li10 {
public static void main(String[] args) {
explainGroupNo();
System.out.println("");
explainGroupQuantifier();
}
public static void explainGroupNo() {
String email = "webmaster@itcast.net";
String regex = "((w+)@([w.]+))";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(email);
if (m.find()) {
System.out.println("match result:t" + m.group(0));
System.out.println("group No.1 is:t" + m.group(1));
System.out.println("group No.2 is:t" + m.group(2));
System.out.println("group No.3 is:t" + m.group(3));
}
}
public static void explainGroupQuantifier() {
String email = "webmaster@itcast.net";
String regex = "(w)+@([w.])+";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(email);
if (m.find()) {
System.out.println("match result:t" + m.group(0));
System.out.println("group No.1 is:t" + m.group(1));
System.out.println("group No.2 is:t" + m.group(2));
}
}
}
运行结果: match result: webmaster@itcast.net
group No.1 is: webmaster@itcast.net
group No.2 is: webmaster
group No.3 is: itcast.net
match result: webmaster@itcast.net
group No.1 is: r
group No.2 is: t
不捕获文本的括号
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li11 {
public static void main(String[] args) {
String email = "webmaster@itcast.net";
String rex="(?:webmaster|admin)@([w.]+)";
Pattern pattern=Pattern.compile(rex);
Matcher matcher=pattern.matcher(email);
if(matcher.find()){
System.out.println("match result:t"+matcher.group(0));
System.out.println("group No.1 is:t" + matcher.group(1));
//System.out.println(matcher.group(2));
}
}
}
运行结果: match result: webmaster@itcast.net
group No.1 is: itcast.net
因为只要出现了括号就会存在捕获分组,并且会保存捕获结果,但是使用(?:…)就不会保存捕获结果了。所以当要输出编号为1的时候就输出了第二个括号的内容。而不会输出第一个括号的捕获内容,因为第一个括号的捕获内容不会保存!!编号0依然是整个正则表达式匹配的内容。 括号的用途:反向引用
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li12 {
public static void main(String[] args) {
String [] string=new String[]{"<h1>呵呵</h1>","<h1>123</h2>"};
String rex="<(w+)>[^<]+<(/1)>";
Pattern pattern=Pattern.compile(rex);
for(String str:string){
Matcher matcher=pattern.matcher(str);
if(matcher.matches()){
System.out.println("字符串:"+"""+str+"""+"匹配""+rex+""");
}else{
System.out.println("字符串:"+"""+str+"""+"不匹配""+rex+""");
}
}
}
}
运行结果: 字符串:"<h1>呵呵</h1>"匹配"<(w+)>[^<]+<(/1)>" 字符串:"<h1>123</h2>"不匹配"<(w+)>[^<]+<(/1)>"
例子:去掉重复单词 import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li13 {
public static void main(String[] args) {
One();
System.out.println();
Two();
}
private static void Two() {
String string="hello hello";
String rex="((w+)s+2)";
Pattern pattern=Pattern.compile(rex);
Matcher matcher=pattern.matcher(string);
System.out.println("字符串:"+string);
System.out.println("去掉后:"+matcher.replaceAll("$2"));//美元符号可视为到如上所述已捕获子序列的引用
}
private static void One() {
String string="java java";
String rex="(w+)s+1";
System.out.println("字符串:"+string);
System.out.println("去掉后:"+string.replaceAll(rex,"$1"));//美元符号可视为到如上所述已捕获子序列的引用
}
}
运行结果: 字符串:java java
去掉后:java
字符串:hello hello
去掉后:hello
到了这个阶段,我想读者也有一定能力了。来看看这个文章: 强调在正则中只要单纯用了括号就会有捕获分组保存 锚点
例子: import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li15 {
public static void main(String[] args) {
String [] strings=new String[]
{"我爱 java ","我爱 html5 ","我爱 "java"","我爱 javase"
};
String rex="bjavab";
Pattern pattern=Pattern.compile(rex);
for(String str:strings){
Matcher matcher=pattern.matcher(str);
if(matcher.find()){
System.out.println("字符串:["+str+"]有");
}else{
System.out.println("字符串:["+str+"]没有");
}
System.out.println();
}
}
}
运行结果: 字符串:[我爱 java ]有
字符串:[我爱 html5 ]没有
字符串:[我爱 "java"]有
字符串:[我爱 javase]没有
注意事项:
锚点二
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class li16 {
public static void main(String[] args) {
String[] strings = new String[] { "start "," start "," end "," end" };
String[] regexes = new String[] { "^start","Astart","end$","endZ"};
for (String str : strings) {
for (String regex : regexes) {
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("");
}
}
}
运行结果: "start " can be matched with regex "^start"
"start " can be matched with regex "Astart"
"start " can not be matched with regex "end$"
"start " can not be matched with regex "endZ"
" start " can not be matched with regex "^start"
" start " can not be matched with regex "Astart"
" start " can not be matched with regex "end$"
" start " can not be matched with regex "endZ"
" end " can not be matched with regex "^start"
" end " can not be matched with regex "Astart"
" end " can not be matched with regex "end$"
" end " can not be matched with regex "endZ"
" end" can not be matched with regex "^start"
" end" can not be matched with regex "Astart"
" end" can be matched with regex "end$"
" end" can be matched with regex "endZ" (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |