Regex for Greek Extended or Greek Script Characters

In this Java regular expression tutorial, we will learn to match any character which is part of “Greek Extended” unicode block or Greek script.

In regex, ‘\p{}‘ syntax is used to match characters based on Unicode properties.

  • \p{InGreek}: Matches any character in the Greek and Coptic Unicode block. (Range: U+0370 to U+03FF)
  • \p{InGreekExtended}: Matches any character in the Greek Extended Unicode block. (Range: U+1F00 to U+1FFF)

Solution Regex(s): \\p{InGreek} and \\p{InGreekExtended}

1. Match Any Character in Greek Script

Let’s look at a program that can match any character in Greek script in a string.

String content = "A math equation might be α + β = λ + γ";
 
String regex = "\\p{InGreek}";
 
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
  System.out.print("Start index: " + matcher.start());
  System.out.print(" End index: " + matcher.end() + " ");
  System.out.println(" : " + matcher.group());
}

The program output:

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

2. Match Any Character in “Greek Extended” Unicode Block

Let’s look at an example program that can match any character in Greek script in a string.

String content = "Let's learn some new greek extended characters : ᾲ , ᾨ etc.";
 
String regex = "\\p{InGreekExtended}";
 
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
  System.out.print("Start index: " + matcher.start());
  System.out.print(" End index: " + matcher.end() + " ");
  System.out.println(" : " + matcher.group());
}

The program output:

Start index: 49 End index: 50 : ᾲ
Start index: 53 End index: 54 : ᾨ

References:

  • http://en.wikipedia.org/wiki/Greek_alphabet
  • http://www.alanwood.net/unicode/greek_extended.html

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.