Java中的正则表达式“ \ G”元字符
子表达式/元字符“\G”与最后一个匹配结束的点匹配。
示例
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main( String args[] ) {
String regex = "\\G[0-9]";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
String input = sc.nextLine();
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
int count = 0;
String digits = "";
System.out.println("上一场比赛的数字:");
while(m.find()) {
System.out.print(m.group());
count ++;
}
System.out.println();
System.out.println("Number of matches: "+count);
}
}输出结果
Enter a string: 555 sample text 上一场比赛的数字: 555 Number of matches: 3
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西