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

Пример поля LITERAL в Java

Включить текстовый анализ шаблона. Здесь все символы (включая последовательности escaping и метасимволы) не имеют никакого особого значения и рассматриваются как литеральные символы.

Например, обычно, если вы ищете регулярное выражение «^ This» в данном тексте ввода, то оно будет соответствовать слову, которое начинается с«This»Начало строки.

Пример

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\n"
         + "This is the second line\n"
         + "^This is the third line";
      // Регулярное выражение принимает даты в формате MM-DD-YYY
      String regex = "^This";
      // Создание объекта Pattern
      Pattern pattern = Pattern.compile(regex, Pattern.LITERAL);
      // Создание объекта Matcher
      Matcher matcher = pattern.matcher(input);
      int count = 0;
      while(matcher.find()) {
         count++;
         System.out.println(matcher.group());
      }
      System.out.println("Количество совпадений: " + count);
   }
}

Результат вывода

^This
Количество совпадений: 1

В текстовом режиме метасимвол «^» не имеет значения, и регулярное выражение «^ This» соответствует точному слову.

Пример

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\n"
         + "This is the second line\n"
         + "^This is the third line";
      // Регулярное выражение принимает даты в формате MM-DD-YYY
      String regex = "^This";
      // Создание объекта Pattern
      Pattern pattern = Pattern.compile(regex, Pattern.LITERAL);
      System.out.println("Обычно это выводится как: 
" + input);
      // Создание объекта 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 first line
This is the second line
^This is the third line
^This
Количество совпадений: 1