package regular;
import java.util.Scanner;
public class RegularDemo1 {
/**
* @正则表达式(引入概念)
*/
public static void main(String[] args) {
checkQQ_2();
}
public static void checkQQ_2(){
Scanner sc = new Scanner(System.in);
String qq = sc.nextLine();
String regex = "[1-9][0-9]{4,14}";
boolean flag = qq.matches(regex);
if(flag)
System.out.println("qq:"+qq +" is OK");
else
System.out.println(qq+":nonono");
}
/*
*/
public static void checkQQ_1(){
Scanner sc = new Scanner(System.in);
String qq = sc.nextLine();
int len = qq.length();
if(len>=5 && len<=15){
if(!qq.startsWith("0")){
/*方法二
try{
Long l = Long.parseLong(qq);
System.out.println("qq:"+qq);
}catch(NumberFormatException e){
System.out.println("出现非法字符");
}
*/
/*方法一
char[] arr = qq.tocharArray();
boolean flag = true;
for(int i=0;i<arr.length;i++){
if(!(arr[i]>='0' && arr[i]<='9')){
flag=false;
break;
}
}
if(flag){
System.out.println("qq:"+qq);
}
else{
System.out.println("出现非法字符");
}
*/
}
else
{
System.out.println("不可以以零开头");
}
}
else
{
System.out.println("长度错误");
}
}
}
原文链接:https://www.f2er.com/regex/360500.html