re+ 元字符在 Java 中的正则表达式
子表达式/元字符 “re+” 匹配前面的表达式的出现一次或多次。
示例 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("Number of matches: "+count);
}
}输出
Number of matches: 4
示例 2
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main( String args[] ) {
String regex = "[0-9]+";
Scanner sc = new Scanner(System.in);
System.out.println("Enter an input string: ");
String input = sc.nextLine();
//Creating a Pattern object
Pattern p = Pattern.compile(regex);
//Creating a Matcher object
Matcher m = p.matcher(input);
if(m.find()) {
System.out.println("Given input contains digits");
} else {
System.out.println("Given input doesn't contain digits");
}
}
}输出 1
Enter an input string: sample text Given input doesn't contain digits
输出 2
Enter an input string: sample data 464 Given input contains digits
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP