#正则表达式
所在包 java.util.regex;
所在类:Pattern(编译正则表达式)
Matcher(效验正则表达式)
1. Pattern类
没有公共的构造方法,若要创建对象只能调用它静态的方法
Pattern p=Pattern.complile(String regex); //参数为正则字符串
Pattern pt=Pattern.complile(String regex,int flags);
//flag参数为给定标志性匹配模式 ,这些标志性都是Pattern类中的属性
//标志性匹配模式有八种
public static final int CASE_INSENSITIVE = 0x02; //启用不区分大小写的模式
public static final int MULTILINE = 0x08; //启用多行模式 表达式^和$分别在输入序列的行终止符或结束之前匹配
public static final int UNIX_LINES = 0x01; // 启用Unix线模式。
在这种模式下,在.、^和$的行为中只识别“n”行终止符。
public static final int COMMENTS = 0x04; // 允许空格和注释的模式。
在这种模式下,空格被忽略,以#开头的嵌入注释被忽略,直到行的末尾。
public static final int LITERAL = 0x10; // 启用模式的文字解析
public static final int DOTALL = 0x20; // 启用dotall模式
public static final int UNICODE_CASE = 0x40; // 启用Unicode感知的案例折叠。
public static final int CANON_EQ = 0x80; // 启用规范等价
常用方法
String regex=p.pattern(); //返回正则字符串
String[ ] str=Pattern.split( CharSequence input);
//参数(input)为需要匹配的字符串 , 该方法会以正则字符串为分隔线,将字符串分隔成一个字符数组,该字符数组不包含正则字符串。
//类似于String.spilt( String regex);
import java.util.regex.Pattern;
public class Zhengze_正则表达式 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Pattern p=Pattern.compile("\\d+"); //该正则字符串为一个或多个数字
String str="我喜欢你:233,你喜欢我吗:2233";
String[] str2=p.split(str); //以数字为分隔线
for(String s:str2){
System.out.println(s);
}
System.out.println(p.pattern()); //返回正则字符串
}
}
Matcher b=p.matcher(str); //matcher方法要配合Matcher类使用
同样Matcher 也没有公共的构造方法
它与Pattern配合使用
Matcher b=p.matcher(str); //matcher方法要配合Matcher类使用
常用方法
public int start(){} //返回字符匹配的之前的索引,可能有些拗口
public int start(int group){} //返回匹配字符在该字符串,第几个捕获组匹配字符的之前的索引
//该方法要配合捕获组使用
public int end(){} //返回匹配字符串的最后一个字符的之后的位置
public int end( int group){} //返回匹配字符匹配到的,第几个捕获组字符串的最后一个字符的之后索引
代码实现
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Zhengze_正则表达式 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Pattern p=Pattern.compile("\\d+"); //该正则字符串为一个或多个数字
String str="我喜欢你:233,你喜欢我吗:2233";
Matcher b=p.matcher(str);
boolean bk=b.find();
int e=b.end(); //表示匹配到的字符的最后一个字符之后的索引
int s=b.start(); //表示匹配到字符之前的索引
System.out.println("end="+e+"start="+s);
}
}
输出
end=8 start=5
使用捕获组的方法
捕获组是由多个单独单元进行处理的方法,它通过对括号内的字符分组来创建
用以下方法捕获组是以你的正则字符串来根据分组的
常用方法:Matcher.group( int index);
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Zhengze_正则表达式 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Pattern p=Pattern.compile("(\\D+)(\\d+)(\\D+)(\\d+)"); //该正则字符串为一个或多个数字
String str="我喜欢你:233,你喜欢我吗:2233";
System.out.println(p.pattern()); //返回正则字符串
Matcher b=p.matcher(str);
b.find();
String str1=b.group(0);
String str2=b.group(1);
String str3=b.group(2);
String str4=b.group(3);
String str5=b.group(4);
int k= b.end(2); //该方法也要配合捕获组使用
int h=b.start(2);
System.out.println("第二组最后一个字符后一位索引"+k+" 第二组第一个字符前的索引"+h);
System.out.println("第一组="+str1+" |第二组 "+str2+" |第三组 "+str3+" |第四组 "+str4+" |第五组 "+str5);
}
}
输出
(\D+)(\d+)(\D+)(\d+)
第二组最后一个字符后一位索引8 第二组第一个字符前的索引5
第一组=我喜欢你:233,你喜欢我吗:2233 |第二组 我喜欢你: |第三组 233 |第四组 ,你喜欢我吗: |第五组 2233
若有需要补充的评论区有时间会回复哦