以下是几个Java正则表达式的示例:
1. 匹配一个数字:
String regex = "\\d";
String input = "3";
boolean isMatch = input.matches(regex); // true
2. 匹配一个日期(格式为YYYY-MM-DD):
String regex = "^\\d{4}-\\d{2}-\\d{2}$";
String input = "2023-03-01";
boolean isMatch = input.matches(regex); // true
3. 匹配一个邮箱地址:
String regex = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$";
String input = "example@gmail.com";
boolean isMatch = input.matches(regex); // true
4. 匹配一个电话号码(格式为xxx-xxxxxxx或者xxxx-xxxxxxxx):
String regex = "^(\\d{3}-\\d{7}|\\d{4}-\\d{8})$";
String input = "021-1234567";
boolean isMatch = input.matches(regex); // true
5. 匹配一个IP地址:
String regex = "^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$";
String input = "192.168.0.1";
boolean isMatch = input.matches(rege);//true