字符类型:并集 - Java 正则表达式
Java 正则表达式中的字符类型使用方括号“[]”定义,此子表达式匹配指定的一组可能字符中的一个字符。例如,正则表达式 [abc] 匹配单个字符 a、b 或 c。
字符类型的并集变量允许您从指定范围中的某个字符进行匹配,即表达式 [a-z[0-9]] 匹配的单个字符可能是一个小写字母 (a-z) 或一个数字 (0-9)。
示例
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter input text: ");
String input = sc.nextLine();
String regex = "[a-z[0-9]]";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(input);
int count =0;
while (matcher.find()) {
count++;
}
System.out.println("Number characters from the ranges (a-z or 0-9): "+count);
}
}输出
Enter input text: sample DATA 12345 Number characters from the ranges (a-z or 0-9): 11
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP