English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Experience NotesTextjava
java.util.regexThe package provides various classes to find specific patterns in character sequences. The pattern class in this package is the compiled representation of regular expressions.match()-
The method of the pattern class accepts
Represents the string value of the regular expression.CharSequence
An object of the class represents the input string.
Example import java.util.Scanner; import java.util.regex.Pattern; public class MatchesExample { //public static void main(String[] args) { Get date Scanner sc = new Scanner(System.in);/mm/System.out.println("Enter date string in [dd yyy] format: "); String date = sc.next();1[0-2]|0[1-9])/String regex = "^(3[01(12]|[-9]|0[1-9])/[0-9]{4$"; //Create a pattern object boolean result = Pattern.matches(regex, date); if(result) { System.out.println("Date is valid"); } else { System.out.println("Date is not valid"); } } }
Enter date string in [dd/mm/yyy] format: 01/12/2019 Date is valid
Enter date string in [dd/mm/yyy] format: 2019-21-12 Date is not valid