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

예제를 포함한 Java의 UNIX_LINES 필드를 패턴화

이 표지는 Unix 행 모드를 활성화합니다. Unix 행 모드에서는 '\n'만 행 종료 기호로 사용하고 '\r'를 텍스트 문자로 간주합니다.

예제1 

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LTERAL_Example {
   public static void main(String[] args) {
      String input = "This is the first line\r"
         + "This is the second line\r"
         + "This is the third line\r"
      //정규 표현식은 MM-DD-YYY 형식이 날짜를 받아들입니다
      String regex = "^T.*e";
      //Pattern 객체를 생성합니다
      Pattern pattern = Pattern.compile(regex, Pattern.UNIX_LINES);
      //Matcher 객체를 생성합니다
      Matcher matcher = pattern.matcher(input);
      int count = 0;
      while(matcher.find()) {
         count++;
         System.out.println(matcher.group());
      }
      System.out.println("매치 횟수: ");+count);
   }
}

출력 결과

이것은 첫 번째 줄입니다
This is the second line
This is the third line
매치 횟수: 1

而已제 상태에서, \r는 개행으로 간주됩니다.

예제2

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LTERAL_Example {
   public static void main(String[] args) {
      String input = "This is the first line\r"
         + "This is the second line\r"
         + "This is the third line\r"
      //정규 표현식은 MM-DD-YYY 형식이 날짜를 받아들입니다
      String regex = "^T.*e";
      //Pattern 객체를 생성합니다
      Pattern pattern = Pattern.compile(regex);
      //Matcher 객체를 생성합니다
      Matcher matcher = pattern.matcher(input);
      int count = 0;
      while(matcher.find()) {
         count++;
         System.out.println(matcher.group());
      }
      System.out.println("매치 횟수: ");+count);
   }
}

출력 결과

이것은 첫 번째 줄입니다
매치 횟수: 1