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

Java의 패턴 DOTALL 필드 예제

Pattern 클래스의 DOTALL 필드는 dotall 모드를 활성화합니다. 기본적으로, "." 정규 표현식의 메타 문자는 행 종료 기호를 제외한 모든 문자와 일치합니다.

예제1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DOTALL_Example {
   public static void main(String args[]) {
      String regex = ".";
      String input = "this is a sample \nthis is second line";
      Pattern pattern = Pattern.compile(regex);
      Matcher matcher = pattern.matcher(input);
      int count = 0;
      while(matcher.find()) {
         count++;
         System.out.print(matcher.group());
      }
      System.out.println();
      System.out.println("newline character 수: \n"+count);
   }
}

출력 결과

this is a sample this is second line
newline character 수:
36

모든 문자, 행 종료 문자를 포함하여 모든 문자와 매칭합니다.

다시 말해, 이를 사용할 때compile()메서드의 기호 값이 "." 문자와 "\n" 포함된 모든 문자를 매칭합니다.

예제2

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DOTALL_Example {
   public static void main(String args[]) {
      String regex = ".";
      String input = "this is a sample \nthis is second line";
      Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
      Matcher matcher = pattern.matcher(input);
      int count = 0;
      while(matcher.find()) {
         count++;
         System.out.print(matcher.group());
      }
      System.out.println();
      System.out.println("newline character 수: \n"+count);
   }
}

출력 결과

이는 샘플입니다
이는 두 번째 줄입니다
newline character 수:
37