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 : ₹
Start index: 34 End index: 35 : ₹
That’s all about regex for currency symbols in java.