// 判断数字
public static boolean isNumberic(String s) {
return Pattern.compile("([1-9]+|0)\\.*[0-9]+").matcher(s).matches();
}
// 判断邮箱
public static boolean isEmail(String s) {
return Pattern.compile("\\w+@{1}\\w+\\.{1}\\w+").matcher(s).matches();
}
// 包含某个字符
public static boolean contains(String s, String c) {
return Pattern.compile(".*" + c + "+.*").matcher(s).matches();
}
// 以某个字符串开头
public static boolean startWith(String s, String c) {
return Pattern.compile("^" + c + "\\w*").matcher(s).matches();
}
// 以某个字符串结尾
public static boolean endWith(String s, String c) {
return Pattern.compile("\\w*" + c + "$").matcher(s).matches();
}
java n正则初步
最新推荐文章于 2024-04-25 16:40:14 发布
本文介绍了几种使用正则表达式进行字符串验证的方法,包括数字、邮箱格式的校验及字符串包含、开头和结尾特定字符的判断。这些方法在实际开发中非常实用,可以帮助开发者快速准确地完成数据验证工作。

541

被折叠的 条评论
为什么被折叠?



