程序员

Java正则表达式

2018-08-16  本文已影响16人  LeaveStyle

Java正则表达式语法规则

//代码示例
<form action="/servlet" method="post">
    用户名:<input type="text" name="username" pattern="[a-zA-Z]{6,12}" required="required" placeholder="请输出6-12位的数字或字母"/>
    密&nbsp码:<input type="password" name="password" pattern="[0-9]{6,}" required="required" placeholder="密码为至少6位的数字"/>、
    手机号:<input type="text" name="phone" pattern="1[3578]\d{9}" required="required" placeholder="请输入正确的手机号"/>
    邮&nbsp箱:<input type="email" name="email" required="required" placeholder="请输入邮箱"/>
     <input type="submit" value="注册"/>
     <input type="reset" value="重置"/>
</form>
//代码示例
public class RegexText{
    public static void main(String[] args){
        String data = "a"; //给定一个非数字
        String regex = "\\D"; //非数字
        boolean flag = data.matches(regex);
        System.out.println(flag); //结果为true

        String data2 = " ";
        String regex2 = "\\s"; //空白字符  
        boolean flag2 = data2.matches(regex2);
        System.out.println(flag2); //结果为true

        String data3 = " abc";
        String regex3 = "\\S"; //非空白字符  
        boolean flag3 = data3.matches(regex3);
        System.out.println(flag3); //结果为true

        String data4 = "8";
        String regex4 = "\\w"; //单词字符  :[a-zA-Z_0-9]
        boolean flag4 = data4.matches(regex4);
        System.out.println(flag4); //结果为true
    }
}
上一篇 下一篇

猜你喜欢

热点阅读