Character Class \p{javaMirrored} จับคู่ตัวอักษรที่ Character.isMirrored(char) คืนค่า true คือ ตัวอักษรที่มี "mirrored" property ใน Unicode (ใช้สำหรับ bidirectional text)
ตัวอย่าง 1: นับ mirrored characters
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string");
String input = sc.nextLine();
String regex = "[\\p{javaMirrored}]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
int count = 0;
while (matcher.find()) {
count++;
}
System.out.println("Number of mirrored characters: " + count);
}
}
ผลลัพธ์
Enter a string: <hello< (how are you) {welcome}
Number of mirrored characters: 6
ตัวอย่าง 2: หาสตริงที่ประกอบด้วย mirrored characters เท่านั้น
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String args[]) {
String regex = "^\\p{javaMirrored}+$";
Scanner sc = new Scanner(System.in);
System.out.println("Enter 5 input strings: ");
String[] input = new String[5];
for (int i = 0; i < 5; i++) {
input[i] = sc.nextLine();
}
Pattern p = Pattern.compile(regex);
System.out.println("Strings with mirrored characters:");
for (int i = 0; i < 5; i++) {
Matcher m = p.matcher(input[i]);
if (m.matches()) {
System.out.println(m.group());
}
}
}
}
ผลลัพธ์
Enter 5 input strings:
{Raju}
<RAMU>
rahman
radha
{}<>()[]
Strings with mirrored characters:
{}<>()[]
อธิบาย
\p{javaMirrored}= ตัวอักษรที่Character.isMirrored()== true- รวม: วงเล็บ () [] {} <> และสัญลักษณ์ที่มี mirror ใน RTL contexts
- ใน "
<hello< (how are you) {welcome}": <, <, (, ), {, } = 6 ตัว {Raju}มี R,a,j,u ไม่ใช่ mirrored → ไม่ผ่าน{}<>()[]ทุกตัวเป็น mirrored → ผ่าน
Mirrored Characters หลัก (Unicode Bidi_Mirrored=Yes)
| Char | Name | Mirror |
|---|---|---|
| ( | Left Parenthesis | ) |
| ) | Right Parenthesis | ( |
| [ | Left Square Bracket | ] |
| ] | Right Square Bracket | [ |
| { | Left Curly Bracket | } |
| } | Right Curly Bracket | { |
| < | Less-Than Sign | > |
| > | Greater-Than Sign | < |
| « | Left Angle Quote | » |
| » | Right Angle Quote | « |
| ‹ | Single Left Quote | › |
| › | Single Right Quote | ‹ |