获取所有匹配的Java正则表达式的列表
Java没有提供任何方法来检索我们需要使用列表并将所有结果添加到while循环中的所有匹配列表。
示例
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ListOfMatches{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter input text: ");
String input = sc.nextLine();
String regex = "\\d+";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
ArrayList list = new ArrayList();
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
list.add(matcher.group());
}
Iterator it = list.iterator();
System.out.println("List of matches: ");
while(it.hasNext()){
System.out.println(it.next());
}
}
}输出结果
Enter input text: sample 1432 text 53 with 363 numbers List of matches: 1432 53 363
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短