Java regex for currency symbols

In this post, we will see about regex to find currency symbols in a text.

You can use below regex to find currency symbols in any text.

\\p{Sc}

Each unicharacter belongs to certain category and you can search for it using /p. Sc is short code for current symbol, so using \p{Sc}, we are trying to find currency symbol in a text.

package org.arpit.java2blog;

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

public class RegexCurrencySymbol {

    public static void main(String args[]) {
        String text = "Currency symobols are : $ Dollar, ₹ Rupees ";

        String regex = "\\p{Sc}";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        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: 24 End index: 25 : $
Start index: 34 End index: 35 : ₹

That’s all about regex for currency symbols in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.