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

Как использовать Java RegEx для匹配 начала определенной строки/строки?

The meta-character "^" matches the beginning of a specific string, that is, it matches the first character of the string. For example,

  • Expression " ^ \\ d ”Match strings that start with a number.

  • Expression " ^ [az] ”Match strings that start with a lowercase letter.

Пример 1

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
   public static void main(String args[]) {
      //从用户读取字符串
      System.out.println("Enter a String");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      String regex =  "^[^a-zA-Z0-9//s].*";
      //编译正则表达式
      Pattern pattern = Pattern.compile(regex);
      //检索匹配器对象
      Matcher matcher = pattern.matcher(input);
      if(matcher.matches()) {
         System.out.println("Match occurred");
      } else { 
         System.out.println("Match not occurred");
      }
   }
}

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

Enter a String
#starting with a special character
Match occurred

Пример 2

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 = \
      Scanner sc = new Scanner(System.in);
      System.out.println("Введите 5 строк ввода:");
      String input[] = new String[5];
      for(int i = 0; i < 5; i++) {
         input[i] = sc.nextLine();
      }
      // Создается объект Pattern
      Pattern p = Pattern.compile(regex);
      for(int i = 0; i < 5; i++) {
         // Создается объект Matcher
         Matcher m = p.matcher(input[i]);
         if(m.find()) {
            System.out.println("String " + i + " ends with '.'");
         }
      }
   }
}

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

Введите 5 строк ввода:
hello how are you.
where do you live
what is your name.
welcome to w3codebox
The Biggest Online Tutorials Library.
String 0 ends with '.'
String 2 ends with '.'
String 4 ends with '.'

Пример 3

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 = "^[A-Z]";
      Scanner sc = new Scanner(System.in);
      System.out.println("Введите 5 строк ввода:");
      String input[] = new String[5];
      for(int i = 0; i < 5; i++) {
         input[i] = sc.nextLine();
      }
      // Создается объект Pattern
      Pattern p = Pattern.compile(regex);
      for(int i = 0; i < 5; i++) {
         // Создается объект Matcher
         Matcher m = p.matcher(input[i]);
         if(m.find()) {
            System.out.println("Строка " + i + " начинается с заглавной буквы");
         }
      }
   }
}

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

Введите 5 строк ввода:
Пример текста1
Пример текста2
Привет, как ты?
Добро пожаловать в w3codebox
Доброе утро
Строка 0 начинается с заглавной буквы
Строка 3 начинается с заглавной буквы
Строка 4 начинается с заглавной буквы
Вам может понравиться