使用Java中的Regex从字符串中提取每个单词
一个单词代表从a到z或A到Z的连续字母。使用正则表达式匹配来自az和AZ的任何字母就足够了。我们将使用以下正则表达式模式-
[a-zA-Z]+
[az]匹配从a到z的任何字符。
[AZ]匹配从A到Z的任何字符。
+匹配群组中的1或更多字符。
示例
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Tester {
public static void main(String[] args) {
String candidate = "this is a test, A TEST.";
String regex = "[a-zA-Z]+";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(candidate);
System.out.println("INPUT: " + candidate);
System.out.println("REGEX: " + regex + "\r\n");
while (m.find()) {
System.out.println(m.group());
}
}
}这将产生以下结果-
输出结果
this is a test A TEST
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿