Java 中的正则表达式"G"元字符
javaobject oriented programmingprogramming更新于 2024/5/5 2:20:17
子表达式/元字符"\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("输入一个字符串:");
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("匹配项数: "+count);
}
}
输出
输入字符串: 555 sample text 上一个匹配项中的数字: 555 匹配项数:3
相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

