सबएक्सप्रेशन/मेटाकैरेक्टर “re*” पूर्ववर्ती एक्सप्रेशन की 0 या अधिक घटनाओं से मिलान करता है।
उदाहरण 1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main( String args[] ) {
String regex = "aabc*";
String input = "aabcabcaabcabbcaabcbcaabc";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
int count = 0;
while(m.find()) {
count++;
}
System.out.println("मिलान की संख्या: " + count);
}
}
आउटपुट
मिलान की संख्या: 4
उदाहरण 2
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchAllCharacters {
public static void main( String args[] ) {
String regex = "(.*)?(\\d+)";
Scanner sc = new Scanner(System.in);
System.out.println("5 इनपुट स्ट्रिंग दर्ज करें: ");
String input[] = new String[5];
for (int i=0; i<5; i++) {
input[i] = sc.nextLine();
}
// Pattern ऑब्जेक्ट बनाना
Pattern p = Pattern.compile(regex);
System.out.println("अंक वाली स्ट्रिंग: ");
for(int i=0; i<5;i++) {
// Matcher ऑब्जेक्ट बनाना
Matcher m = p.matcher(input[i]);
if(m.matches()) {
System.out.println(m.group());
}
}
}
}
आउटपुट
test data hello how are you 335 welcome to Tutorialspoint अंक वाली स्ट्रिंग: sample text 1 hello how are you 335