Posix 字符类 p{InGreek} Java 正则表达式
javaobject oriented programmingprogramming更新于 2025/7/13 10:20:17
此类 \p{InGreek} 匹配希腊字符。
示例
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example1 {
public static void main(String args[]) {
//从用户读取字符串
System.out.println("输入字符串");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
//正则表达式
String regex = "\p{InGreek}";
//编译正则表达式
Pattern pattern = Pattern.compile(regex);
//检索匹配器对象
Matcher matcher = pattern.matcher(input);
int count = 0;
while(matcher.find()) {
count++;
}
System.out.println("希腊字母数量: "+count);
}
}
输出
输入字符串 示例文本 αγ ηθ ξψ 希腊字母数量: 6
示例
import java.util.Scanner;
public class Example {
public static void main(String args[]) {
//从用户读取字符串
System.out.println("输入字符串");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
//正则表达式
String regex = "^.+\p{InGreek}.+";
boolean result = input.matches(regex);
if(result) {
System.out.println("给定字符串包含希腊字符");
} else {
System.out.println("给定字符串不包含希腊字符");
}
}
}
输出
输入字符串 sample text αγ 给定字符串包含希腊字符
相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

