Java Regex for Currency Symbols

In this tutorial, we will learn to match all available currency symbols, e.g. $ Dollar, € Euro, ¥ Yen, in some text content.

Solution Regex :  \\p{Sc}

Example Code for matching any Currency Symbol using Java Regex

String content = "Let's find the symbols or currencies : $ Dollar, € Euro, ¥ Yen";
      
String regex = "\\p{Sc}";

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());
}

Output:

Start index: 39 End index: 40  : $
Start index: 49 End index: 50  : €
Start index: 57 End index: 58  : ¥

Happy Learning !!

Comments are closed for this article!

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.