注:为纯演示代码,so,变量命名不要在意
java.util.regex.Pattern
类中最常用的方法是matches(String regex, CharSequence input)
、split(CharSequence input)
、split(CharSequence input, int limit)
。
matches方法
public static boolean matches(String regex, CharSequence input)
编译给定的正则表达式并尝试将给定的输入与它进行匹配。
使用如下方式进行调用
Pattern.matches(regex, input);
与以上方法完全一样
Pattern.compile(regex).matcher(input).matches()
如果要多次使用某个模式,编译一次并重用它将比每次调用此方法更有效。
Parameters:
regex - 要编译的表达式
input - 要匹配的字符序列
Throws:
PatternSyntaxException - 如果表达式的语法无效
测试代码
import java.util.regex.Pattern;
public class PatternTest {
public static void main(String[] args) {
matchesTest();
}
public static void matchesTest() {
String s = "aaavvv555";
System.out.println(Pattern.matches("[a-z0-9]+",s));
}
}
输出结果为true
。
split方法
在java.util.regex.Pattern
类中,拥有split(CharSequence input)
、split(CharSequence input, int limit)
两个方法。
首先是 split(CharSequence input, int limit)
的练习:
public String[] split(CharSequence input, int limit)
将给定的输入序列匹配后分割为指定模式。
此方法返回的数组包含输入序列的每个子字符串,该子字符串由与此模式匹配的另一个子序列
终止,或者由输入序列的结尾终止。数组中的子字符串按输入中出现的顺序排列。如果这个模
式不匹配输入的任何子序列,那么结果数组只有一个元素,即字符串形式的输入序列。
limit参数控制模式应用的次数,因此影响结果数组的长度。如果n大于零,那么该模式将最
多应用n-1次,数组的长度不会大于n,并且数组的最后一个条目将包含超出最后匹配定界符
的所有输入。如果n是非正数,那么该模式将尽可能多地应用,并且该数组可以具有任何长
度。如果n为零,那么该模式将尽可能多次应用,数组可以有任意长度,并且尾部的空字符串
将被丢弃。
"boo:and:foo"作为输入序列,然后以指定模式输出
分隔符 | 应用次数 | 结果 |
---|---|---|
: | 2 | { “boo”, “and:foo” } |
: | 5 | { “boo”, “and”, “foo” } |
: | -2 | { “boo”, “and”, “foo” } |
o | 5 | { “b”, “”, “:and:f”, “”, “” } |
o | -2 | { “b”, “”, “:and:f”, “”, “” } |
o | 0 | { “b”, “”, “:and:f” } |
参数:
input - 要分割的字符序列
limit - 模式应用次数
Returns:
通过将输入分解为该模式的匹配来计算的字符串数组
测试代码:
import java.util.regex.Pattern;
public class PatternTest {
public static void main(String[] args) {
splitTest();
}
public static void splitTest() {
String s = "boo:and:foo";
String[] sp = Pattern.compile(":").split(s,2);
for(int i = 0;i < sp.length;i++) {
System.out.print("\""+sp[i]+"\""+", ");
}
System.out.println();
String[] sp1 = Pattern.compile(":").split(s,5);
for(int i = 0;i < sp1.length;i++) {
System.out.print("\""+sp1[i]+"\""+", ");
}
System.out.println();
String[] sp2 = Pattern.compile(":").split(s,-2);
for(int i = 0;i < sp2.length;i++) {
System.out.print("\""+sp2[i]+"\""+", ");
}
System.out.println();
String[] sp3 = Pattern.compile("o").split(s,5);
for(int i = 0;i < sp3.length;i++) {
System.out.print("\""+sp3[i]+"\""+", ");
}
System.out.println();
String[] sp4 = Pattern.compile("o").split(s,-2);
for(int i = 0;i < sp4.length;i++) {
System.out.print("\""+sp4[i]+"\""+", ");
}
System.out.println();
String[] sp5 = Pattern.compile("o").split(s,0);
for(int i = 0;i < sp5.length;i++) {
System.out.print("\""+sp5[i]+"\""+", ");
}
System.out.println();
}
}
输出结果为:
"boo", "and:foo",
"boo", "and", "foo",
"boo", "and", "foo",
"b", "", ":and:f", "", "",
"b", "", ":and:f", "", "",
"b", "", ":and:f",
接下来是 split(CharSequence input)
的练习:
public String[] split(CharSequence input)
将给定的输入序列匹配后分割为指定模式。
此方法的工作原理与通过使用给定输入序列和无limit参数调用双参数拆分方法一样。
尾部的空字符串因此不包含在结果数组中。
"boo:and:foo"作为输入序列,然后以指定模式输出:
分隔符 | 结果 |
---|---|
: | { “boo”, “and”, “foo” } |
o | { “b”, “”, “:and:f” } |
参数:
input - 要分割的字符序列
Returns:
通过将输入分解为该模式的匹配来计算的字符串数组
测试代码
import java.util.regex.Pattern;
public class PatternTest {
public static void main(String[] args) {
splitTest2();
}
public static void splitTest2() {
String s = "boo:and:foo";
String[] sp = Pattern.compile(":").split(s);
for(int i = 0;i < sp.length;i++) {
System.out.print("\""+sp[i]+"\""+", ");
}
System.out.println();
String[] sp1 = Pattern.compile("o").split(s);
for(int i = 0;i < sp1.length;i++) {
System.out.print("\""+sp1[i]+"\""+", ");
}
System.out.println();
}
}
输出结果为:
"boo", "and", "foo",
"b", "", ":and:f",