\d 数字0-9
\D 非数字[^0-9]
\s 空白字符[ \t\n\x0B\f\r]
\S 非空白字符[^\s]
\w 数字字母下划线
\W [^\w]
出现次数
? 0或1
* >=0
+ >=1
{x} x次
{x,} >=x
{x,y} >=x && <=y
\D 非数字[^0-9]
\s 空白字符[ \t\n\x0B\f\r]
\S 非空白字符[^\s]
\w 数字字母下划线
\W [^\w]
出现次数
? 0或1
* >=0
+ >=1
{x} x次
{x,} >=x
{x,y} >=x && <=y
String testStr = "f3j39fl3k";
String reg = "[0-9]+";
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(testStr);
System.out.println(matcher.matches());//false 检测是否完全匹配
String result = "";
while (matcher.find()) {
result = matcher.group();
System.out.println(result);
/*
* 3
* 39
* 3
*/
}
result = matcher.replaceAll("AAA");
System.out.println(result);//fAAAjAAAflAAAk
本文介绍了一段使用Java进行正则表达式匹配的代码示例。通过具体例子展示了如何在字符串中查找数字序列,并实现了替换操作。文章还探讨了不同正则表达式的含义及其应用场景。
2290

被折叠的 条评论
为什么被折叠?



