सबएक्सप्रेशन/मेटाकैरेक्टर “\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