Nói chung, có 7 ký tự không in được phổ biến và mỗi ký tự có biểu diễn thập lục phân riêng.
| Tên | ký tự | Biểu diễn hệ thập lục phân |
|---|---|---|
| chuông | \ a | 0x07 |
| Thoát | \ e | 0x1B |
| Nguồn cấp dữ liệu biểu mẫu | \ f | 0x0C |
| Nguồn cấp dữ liệu dòng | \ n | 0x0A |
| Trả hàng | \ r | 0X0D |
| Tab ngang | \ t | 0X09 |
| Tab dọc | \ v | 0X0B |
Ví dụ 1
Chương trình Java sau đây chấp nhận một văn bản đầu vào và đếm số khoảng cách tab trong đó -
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter input text: ");
String input = sc.nextLine();
String regex = "\\t";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(input);
int count =0;
while (matcher.find()) {
count++;
}
System.out.println("Number of tab spaces in the given iput text: "+count);
}
} Đầu ra
sample text with tab spaces Number of tab spaces in the given input text: 3
Ví dụ 2
Bạn cũng có thể sử dụng các biểu diễn thập lục phân tương ứng của các ký tự không in được để đối sánh.
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter input text: ");
String input = sc.nextLine();
String regex = "\\x09";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(input);
int count =0;
while (matcher.find()) {
count++;
}
System.out.println("Number of tab spaces in the given iput text: "+count);
}
} Đầu ra
Enter input text: sample data with tab spaces Number of tab spaces in the given input text: 4