English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
La classe java.util.regex.Matcher rappresenta il motore che esegue vari operazioni di match. Questa classe non ha costruttore, può essere utilizzatamatches()
Il metodo della classe java.util.regex.Pattern crea/ottiene l'oggetto di questa classe.
Questo tipo dimatches()Il metodo corrisponde una stringa con un modello espresso da un'espressione regolare (entrambi forniti al momento della creazione di questo oggetto). Nel caso di corrispondenza, questo metodo restituisce true, altrimenti restituisce false. Per garantire che il risultato di questo metodo sia corretto, l'intera area deve avere un elemento corrispondente.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchesExample { public static void main(String args[]) { //Read a string from the user System.out.println("Enter a String"); Scanner sc = new Scanner(System.in); String input = sc.next(); //Regular expression to match words starting with a digit String regex = "^[0-9].*$"; //Compile the regular expression Pattern pattern = Pattern.compile(regex); //Retrieve the matcher object Matcher matcher = pattern.matcher(input); //Check for a match boolean bool = matcher.matches(); if(bool) { System.out.println("The first character is a digit"); } else { System.out.println("The first character is not a digit"); } } }
Output result
Enter a String 4hiipla The first character is a digit