Java简单注册格式验证(正则表达式)

时间:2022-09-24 08:39:22

一、用正则表达式写的简单的注册验证,具体要求如下:

用户注册验证(用户名,密码,昵称,中文名字,手机号,邮箱)所有信息符合以下条件才允许注册:

1.用户名格式:由字母数字下划线组成且开头必须是字母,不能超过16位;

2.密码格式:字母和数字构成,不能超过16位;

3.昵称:字母和数字构成长度大于6小于10;

4.手机号:全数字,长度为11位;

5.邮箱:必须包含@符号;必须包含点;点和@之间必须有字符;

所有信息符合上述条件才允许注册。

二、代码如下:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">package <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.lgl</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.day</span>02<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

import java<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.util</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Scanner</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import java<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.util</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.regex</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Matcher</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
import java<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.util</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.regex</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Pattern</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

public class StringCheck {

public static void main(String[] args) {

Pattern p = null<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
Matcher m = null<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
boolean flg = true<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
Scanner sc = new Scanner(System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.in</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"------用户注册------"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
//匹配用户名
System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"用户名: ---(由字母数字下划线组成且开头必须是字母,不能超过16位)"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
String name = sc<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.nextLine</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
p = Pattern<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.compile</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"[a-zA-Z]{1}[a-zA-Z0-9_]{1,15}"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
m = p<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.matcher</span>(name)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
flg = m<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.matches</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

//匹配密码
System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"密码:---(字母和数字构成,不能超过16位)"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
String pass = sc<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.nextLine</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
p = Pattern<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.compile</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"[a-zA-Z0-9]{1,16}"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
m = p<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.matcher</span>(pass)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
flg = m<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.matches</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

//匹配昵称
System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"昵称: ---(字母和数字构成长度大于6小于10) "</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
String nicheng = sc<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.nextLine</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
p = Pattern<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.compile</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"[a-zA-Z0-9]{7,9}"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
m = p<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.matcher</span>(nicheng)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
flg = m<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.matches</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

//匹配手机号
System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"手机号: ---(全数字,长度为11位)"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
String phone = sc<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.nextLine</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
p = Pattern<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.compile</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"[0-9]{11}"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
m = p<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.matcher</span>(phone)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
flg = m<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.matches</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

//匹配邮箱
System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"邮箱: ---(必须包含@符号;必须包含点;点和@之间必须有字符)"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
String email = sc<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.nextLine</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
p = Pattern<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.compile</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"[@]{1}[a-zA-Z0-9]+[.]+[a-z]+"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
m = p<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.matcher</span>(email)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
flg = m<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.matches</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

if(flg) {
System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"注册成功"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
} else {
System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.out</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.println</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"信息填写不正确!"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
}


sc<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.close</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

}

}</code>