English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Этот флаг включает режим Unix строк. В режиме Unix строк используется только '\n' в качестве разделителя строк, а '\r' рассматривается как литеральный символ.
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("Number of matches: " + count); } }
Результат вывода
This is the first line Это вторая строка This is the third line Number of matches: 1
В нормальном режиме, \r считается символом carriage return.
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("Number of matches: " + count); } }
Результат вывода
This is the first line Number of matches: 1