Java如何使用预定义字符类regex?
在regex中,您还具有许多预定义的字符类,它们为常用字符集提供了一种简写形式。
列表如下:
package org.nhooo.example.regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PredefinedCharacterClassDemo {
public static void main(String[] args) {
// 定义正则表达式,它将搜索后跟f的空格
// 和两个任意字符。
String regex = "\\sf..";
// 编译模式并获得匹配对象。
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(
"The quick brown fox jumps over the lazy dog");
// 找到每一个匹配并打印
while (matcher.find()) {
System.out.format("Text \"%s\" found at %d to %d.%n",
matcher.group(), matcher.start(), matcher.end());
}
}
}该程序输出以下结果:
Text " fox" found at 15 to 19.
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短