正则表达式匹配 e4为非法,匹配 .9为合法
package layer_one.layer_two.layer_three;
import java.util.Scanner;
public class Check {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
while(in.hasNextLine()){
String str=in.nextLine();
isFloat is=new isFloat();
boolean bool=is.match(str);
if(bool){
System.out.printf("valid\n");
}
else{
System.out.printf("invalid\n");
}
}//in
in.close();
}
}
class isFloat{
public boolean match(String str){
String reg2="^.\\d{1,}$";
boolean bool=str.matches(reg2);
return bool;
}
}
修改后的正则表达式匹配 e4为非法,匹配 .9为合法
看一下,好了
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String str = in.nextLine();
isFloat is = new isFloat();
boolean bool = is.match(str);
if (bool) {
System.out.printf("valid\n");
} else {
System.out.printf("invalid\n");
}
} // in
in.close();
}
}
class isFloat {
public boolean match(String str) {
String reg2 = "^\\.{0,1}[0-9]{0,2}$";
boolean bool = str.matches(reg2);
return bool;
}
}