大多数编程语言现在都支持使用正则表达式作为字符串匹配的一种手段,JAVA当然也不例外。
正则表达式规则
所有正则表达式支持的类都定义在java.util.regex包中。此包中还定义了两个主要的类:
- Pattern类:主要定义要使用的表达式对象
- Matcher类:用于进行正则标记与指定内容的匹配操作
正则表达式规则主要存在以下几类标记:
单个字符
- 字符:表示由一位字符组成
- \\:表示转义字符“\”
- \t:表示一个"\t"符号
- \n:匹配换行符号
字符集
- [abc]:表示可能是字符a,字符b,字符c中的任意一位
- [^abc]:表示不是字符a,b,c中的任意一位
- [a-z]:所有的小写字母
- [a-zA-Z]:表示任意的一位字母,不区分大小写
- [0-9]:表示任意的一位数字
简化的字符集表达式
- .:表示任意的一位字符
- \d:等价于“[0-9]”
- \D:等价于“[^0-9]”
- \s:表示任意的空白字符,例如:“\t”,“\n”
- \S:表示任意的非空白字符
- \w:等价于“[a-zA-Z_0-9]”,表示由任意的字母、数字、_组成
- \W:等价于“[^a-zA-Z_0-9]”,表示不是由任意的字母、数字、_组成
边界匹配
^:正则的开始
$:正则的结束
数量表达
- 正则?:表示此正则可以出现0次或1次
- 正则+:表示此正则可以出现1次或1次以上
- 正则*:表示此正则可以出现0次、1次或多次
- 正则{n}:表示此正则正好出现n次
- 正则{n,}:表示此正则出现n次以上(包含n次)
- 正则{n,m}:表示此正则出现n~m次
逻辑运算
- 正则1正则2:正则1判断完成后继续判断正则2
- 正则1 | 正则2:正则1或者正则2有一组满足即可
- (正则):将多个正则作为一组,可以为这一组单独设置出现的次数
String类对正则的支持
虽然java本身提供的正则支持类都在java.util.regex包中,但是从实际使用来看,却何少使用该包中的Pattern或Matcher类进行正则操作,而大部分情况下都会考虑使用java.lang.String类中提供的方法简化正则操作。
boolean matches(String regex) // Tells whether or not this string matches the given regular expression.
String replaceFirst(String regex, String replacement) // Replaces the first substring of this string that matches the given regular expression with the given replacement.
String replaceAll(String regex, String replacement) // Replaces each substring of this string that matches the given regular expression with the given replacement.
String[] split(String regex) // Splits this string around matches of the given regular expression.
String[] split(String regex, int limit) // Splits this string around matches of the given regular expression.
具体操作为:
public class Demo {
public static void main(String[] args) throws Exception {
String str = "abc123def";
String rex = "[a-z]";
System.out.println(str.replaceAll(rex,""));
rex = "\\d+";
String result[] = str.split(rex);
for(int i = 0;i < result.length;++i) {
System.out.println(result[i]);
}
}
}
执行结果为:
123
abc
def
java.util.regex
虽然String能够支持正则表达式的操作,但是正则表达式原始的开发包是java.util.regex,利用该包中的Pattern和Materer类也同样可以实现正则表达式的操作。
Pattern类中存在的方法
String[] split(CharSequence input) // Splits the given input sequence around matches of this pattern.
String[] split(CharSequence input, int limit) // Splits the given input sequence around matches of this pattern.
Matcher类中存在的方法
boolean matches() // Attempts to match the entire region against the pattern.
String replaceAll(String replacement) // Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.
String replaceFirst(String replacement) // Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.
在实际开发中,由于String使用较多,所以一般很少使用Pattern或Matcher类。
Pattern类的主要功能是进行数据的拆分以及为Matcher类对象实例化,同时该类中没有定义构造方法,所以需要利用compile方法进行指定正则表达式的编译操作。同时在Pattern类中定义的方法,在进行参数接受时接收的都是CharSequence接口对象。
这里再看一下Pattern和Matcher的相关方法:
// Pattern
static Pattern compile(String regex) // Compiles the given regular expression into a pattern.
String[] split(CharSequence input) // Splits the given input sequence around matches of this pattern.
String[] split(CharSequence input, int limit) // Splits the given input sequence around matches of this pattern.
Matcher matcher(CharSequence input) // Creates a matcher that will match the given input against this pattern.
// Matcher
boolean matches() // Attempts to match the entire region against the pattern.
String replaceAll(String replacement) // Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.
String replaceFirst(String replacement) // Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.
而若用Pattern和Matcher进行正则表达式操作,之前的代码需要修改为:
import java.util.Arrays;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Demo {
public static void main(String[] args) throws Exception {
String str = "abc123def";
String rex = "[a-z]";
Pattern pat = Pattern.compile(rex);
Matcher mat = pat.matcher(str);
System.out.println(mat.replaceAll(""));
rex = "\\d+";
pat = Pattern.compile(rex);
String result[] = pat.split(str);
for(int i = 0;i < result.length;++i) {
System.out.println(result[i]);
}
}
}
执行结果为:
123
abc
def
从上面的代码来看,执行结果是一样的,不过使用起来不如String对正则表达式的支持好。