62,614
社区成员
发帖
与我相关
我的任务
分享
Pattern p = Pattern.compile("a*b"); //1
Matcher m = p.matcher("aaaaab");
System.out.println(m.matches());
String test1 = "Exec/eXec/exec";
test=test1.replaceAll("(?i)exec", "");
//方法一 使用表达式 "(?i)exec"
String test2 = "Exec/eXec/exec";
Matcher m = Pattern.compile("exec", Pattern.CASE_INSENSITIVE).matcher(test2);
if (m.find()) test2 = m.replaceAll("");
//方法二 Pattern.compile("exec",Pattern.CASE_INSENSITIVE)
字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
[a-z&&[def]] d、e 或 f(交集)
[a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去)
[a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)
Greedy 数量词
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次
Pattern p = Pattern.compile("ab"); //1
Matcher m = p.matcher("aaaaab");
while(m.find()) {
System.out.println(m.group());
}