Khái niệm
\p{Sc} (Symbol Currency) khớp các ký hiệu tiền tệ: $, €, £, ¥, ₹, ₫...
Ví dụ 1: Đếm ký hiệu tiền tệ
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Nhập chuỗi: ");
String input = sc.nextLine();
String regex = "\\p{Sc}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
int count = 0;
while (matcher.find()) {
count++;
}
System.out.println("Số ký hiệu tiền tệ: " + count);
}
}
Nhập chuỗi: euro €, pounds £, dollar $, yen ¥
Số ký hiệu tiền tệ: 4
Ví dụ 2: Lọc chuỗi chứa tiền tệ
String regex = "^.*\\p{Sc}.*$";
// Input: # #$$@ £shelling -> khớp (có $ và £)
// Input: hello sample 243test -> không khớp
Các ký hiệu tiền tệ phổ biến
| Ký hiệu | Tên | Unicode |
|---|---|---|
| $ | Dollar | U+0024 |
| € | Euro | U+20AC |
| £ | Pound | U+00A3 |
| ¥ | Yen/Yuan | U+00A5 |
| ₹ | Rupee | U+20B9 |
| ₫ | Dong | U+20AB |