# 适用于希腊语扩展或希腊语脚本的 Java 正则表达式

> 原文： [https://howtodoinjava.com/regex/java-regex-match-any-character-in-greek-extended-or-greek-script/](https://howtodoinjava.com/regex/java-regex-match-any-character-in-greek-extended-or-greek-script/)

在本教程中，我们将学习匹配“希腊扩展” unicode 块或希腊语脚本一部分的任何字符。

> **解决方案正则表达式：`\\p{InGreek}`和`\p{InGreekExtended}`**

## 匹配希腊文字中的任何字符

让我们看一个示例程序，该程序能够匹配字符串中希腊语脚本中的任何字符。

```java
&nbsp;String content = "A math equation might be α + β = λ + γ";
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
String regex = "\\p{InGreek}";

Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
while (matcher.find())
{
&nbsp;System.out.print("Start index: " + matcher.start());
&nbsp;System.out.print(" End index: " + matcher.end() + " ");
&nbsp;System.out.println(" : " + matcher.group());
}

Output:

Start index: 25 End index: 26&nbsp; : α
Start index: 29 End index: 30&nbsp; : β
Start index: 33 End index: 34&nbsp; : λ
Start index: 37 End index: 38&nbsp; : γ

```

## 匹配“希腊扩展” unicode 块中的任何字符

让我们看一个示例程序，该程序能够匹配字符串中希腊语脚本中的任何字符。

```java
String content = "Let's learn some new greek extended characters : ᾲ , ᾨ etc.";
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
String regex = "\\p{InGreekExtended}";

Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
while (matcher.find())
{
&nbsp;System.out.print("Start index: " + matcher.start());
&nbsp;System.out.print(" End index: " + matcher.end() + " ");
&nbsp;System.out.println(" : " + matcher.group());
}

Output:

Start index: 49 End index: 50&nbsp; : ᾲ
Start index: 53 End index: 54&nbsp; : ᾨ

```

#### 参考：

[http://en.wikipedia.org/wiki/Greek_alphabet](https://en.wikipedia.org/wiki/Greek_alphabet)

[http://www.alanwood.net/unicode/greek_extended.html](http://www.alanwood.net/unicode/greek_extended.html)

[https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html](https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html)