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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.