1、判断字符串:
例如要输入的字符串由3个字母组成:
第一个字母是a/b/c
第二个字母是d/e/f/g
第三个字母是x/y/z
System.out.println(str.matches("[abc][defg][xyz]"));
—————————————————————————————————
2、匹配由一个字母组成的字符串
System.out.println(str.matches("[a-zA-Z]"));
3、 匹配由一个数字组成的字符串
System.out.println(str.matches("[0-9]"));
System.out.println(str.matches("\\d"));
//\\d表示和上面的0-9一样,是判断str是否是一个属于0-9之内的数字
—————————————————————————————————
4、匹配由一个字符组成的字符串,但不是a/b/c
[^…] 表示除了这些字符
System.out.println(str.matches("[^abc]"));
5、匹配由a开头的由2个字符组成的字符串
. 表示通配符,可以匹配任意一个类型的字符
System.out.println(str.matches("a."));
//判断是否是一个 .
// \\. Java先转义为\. ,正则再转义为.
System.out.println(str.matches("\\."));
6、怎么匹配 \ “c:\java”
// \\\\ Java先转义为\\ ,正则再转义为\
System.out.println(str.matches("\\\\"));
—————————————————————————————————
7、数量词
//+ 表示之前的字符至少出现1次 >= 1
System.out.println(str.matches("a.+"));
// 匹配由小写字母开头由数字结尾的字符串
// * 表示之前的字符可有可无 >= 0
System.out.println(str.matches("[a-z].*\\d"));
//匹配由a开头至多2个字符组成的字符串
// ? 表示之前的字符至多出现1次 <= 1
System.out.println(str.matches("a.?")); // a ab
—————————————————————————————————
// 匹配由5个小写字母组成的字符串
// {n} 表示之前的字符恰好出现n次 == n
System.out.println(str.matches("[a-z]{5}"));acccc
// 匹配至少由5个小写字母组成的字符串
System.out.println(str.matches("[a-z]{5,}"));
// 匹配由8-12个小写字母组成的字符串
System.out.println(str.matches("[a-z]{8,12}"));
—————————————————————————————————
正则表达式练习:
//输入一个字符串,然后判断字符串是否是一个小数字符串
//12.23 0.56 30. 0.36 3.00
//[0-9]+\\.[0-9]+
public static void test3(){
Scanner sc=new Scanner(System.in);
System.out.println("input:");
String str=sc.next();
String s="[1-9]+\\.[0-9]+";
if(str.matches(s)||str.matches("0\\.[0-9]+")){
System.out.println("yes! "+str);
}else{
System.err.println("NOOOOO!");
test3();
}
}
//校验密码:8-20,小写字母、大写字母、数字至少两种
//([a-z]*[0-9]*)+[A-Z]*
//([a-z]+[A-Z]+){8,20}&&([a-z]+[0-9]+){8,20}&&([A-Z]+[0-9]+){8,20}&&([A-Z]+[0-9]+[a-z]){8,20}
public static void test4(){
Scanner sc=new Scanner(System.in);
System.out.println("input:");
String str=sc.next();
int count=0;
String str2="[A-Z0-9a-z]{8,20}";
if(str.matches(".*[A-Z]+.*")){
count++;
}
if(str.matches(".*[a-z]+.*")){
count++;
}
if(str.matches(".*[0-9]+.*")){
count++;
}
if(count>=2&&str.matches(str2)){
System.out.println("YES! 密码:"+str);
}else{
System.out.println("NOOO!");
test4();
}
}
`//练习:邮箱的验证
//[email protected]
//[email protected]
//[email protected]
//[email protected]
//[a-zA-Z0-9_]+@[a-z0-9]{2,}(\\.[comn]{2,3}){1,2}
//0或者1个使用?
public static void test2(){
Scanner sc=new Scanner(System.in);
System.out.println("请输入email:");
String email=sc.next();
String regex="[a-zA-Z0-9_]+@[a-z0-9]{2,}(\\.[comn]{2,3}){1,2}";
if(email.matches(regex)){
System.out.println("email:"+email);
}else{
System.err.println("error!");
test2();
}
}
`
//正则表达式
public static void test1(){
//验证电话号码
Scanner sc=new Scanner(System.in);
System.out.println("input the phone:");
String phone =sc.next();
if(phone.matches("1[356789][0-9]{9}")){
System.out.println("true"+phone);
}else{
System.out.println("error!");
test1();
}
}
5.字符串replaceAll方法
//字符串replaceAll方法
public static void test5(){
String str ="a b c";//去掉空格
String str2 ="asdqsbc";
System.out.println(str.replaceAll("\\s", "*"));
System.out.println(str2.replaceAll("qsb","*" ));
}
6.从一串字符串中,调取到自己想要的信息
比如:
//"0001 张三 18 100 99 100 //0002
//http://127.0.0.1:8080/myweb/index.html?name=admin&pw=123456
//"0001 张三 18 100 99 100
//0002
//http://127.0.0.1:8080/myweb/index.html?name=admin&pw=123456
public static void test6(){
String stu ="001 张三 18";
String[] values=stu.split("\\s");
String name =values[1];
int age =Integer.parseInt(values[2]);
System.out.println(name+","+age);
for(String str:values){
System.out.println(str);
}
}
7.解析一段数据连接
public static void test7(){
String url="http://127.0.0.1:8080/myweb/imdex.html?name=admin&pw=123456";
int index=url.indexOf("?");
if(index==-1) {
System.out.println("没有可以解析的参数");
}else {
String params=url.substring(index+1);
String[] paramArray=params.split("&");
for(String values:paramArray) {
String[] mapArray=values.split("=");
System.out.println(mapArray[0]+":"+mapArray[1]);
}
}
}