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

Java regular expression \t character

Expression sous-chaîne/Caractère de métacaractère " \t correspond aux espaces de tabulation.

Exemple1

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("Entrez une chaîne : ");
      String input = sc.nextLine();
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(input);
      int count = 0;
      while(m.find()) {
         count ++;
      }
      System.out.println("Nombre d'espaces de tabulation : ");+count);
   }
}

Résultat de la sortie

Entrez une chaîne :
Cette phrase contient des espaces de tabulation
Nombre d'espaces de tabulation : 6

Exemple2

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("Entrez une chaîne : ");
      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("Résultat : ");+result);
   }
}

Résultat de la sortie

Entrez une chaîne :
Cette phrase contient des espaces de tabulation
Résultat : Cette phrase contient des espaces de tabulation