일반적으로 인쇄할 수 없는 7개의 일반적인 문자가 사용되며 각 문자에는 고유한 16진수 표현이 있습니다.
| 이름 | 문자 | 16진수 표현 |
|---|---|---|
| 벨 | \a | 0x07 |
| 이스케이프 | \e | 0x1B |
| 양식 피드 | \f | 0x0C |
| 줄 바꿈 | \n | 0x0A |
| 캐리지 리턴 | \r | 0X0D |
| 가로 탭 | \t | 0X09 |
| 세로 탭 | \v | 0X0B |
예시 1
다음 Java 프로그램은 입력 텍스트를 수락하고 그 안에 있는 탭 공백 수를 계산합니다. -
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);
}
} 출력
sample text with tab spaces Number of tab spaces in the given input text: 3
예시 2
인쇄할 수 없는 문자의 각 16진수 표현을 사용하여 일치시킬 수도 있습니다.
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);
}
} 출력
Enter input text: sample data with tab spaces Number of tab spaces in the given input text: 4