Java Regex for Matching any Currency Symbol Example

This Java Regex example demonstrates how to match all available currency symbols, e.g. $ Dollar, € Euro, ¥ Yen, in some text content.

Java Regex for Matching any Currency Symbol Example

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestDemo {
    public static void main(String[] args) {

        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  : ¥


Comments