English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java 정규 표현식의 \ t 유닛 문자

부분 표현식/정규 표현식 문자 " \t ”와 탭 공간과 일치합니다。

예제1

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main( String args[] ) {
      String regex = "\\t";
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a string: ");
      String input = sc.nextLine();
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(input);
      int count = 0;
      while(m.find()) {
         count ++;
      }
      System.out.println("Number of tab spaces: ");+count);
   }
}

출력 결과

Enter a string:
This    is    a    sentence    with    tab    spaces
Number of tab spaces: 6

예제2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main( String args[] ) {
      String regex = "\\t";
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a string: ");
      String input = sc.nextLine();
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(input);
      int count = 0;
      String result = "";
      while(m.find()) {
         result = m.replaceAll(" ");
      }
      System.out.println("Result: ");+result);
   }
}

출력 결과

Enter a string:
This is a sentence with tab spaces
Result: This is a sentence with tab spaces
좋아할 만한 것