Java工具类之Pattern和Matcher(一)

我们在web开发时,肯定会涉及到数据校验,这时正则表达式就必须用到了。在Java中有个Pattern类型对某种正则表达式编译,

,然后使用Matcher类进行判断是否匹配等。其中String类中也有match();使用。这个对Pattern和Matcher的简单使用。

package three.day.util.my;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringTs {


public static void main(String[] argv) throws IOException{
String regex = Pattern.quote("azAZ19@az19.az");
System.out.println(regex);
Pattern pattern = Pattern.compile(regex);
CharSequence input = "1130141391@qq.com";
Matcher matchaer = pattern.matcher(input );
boolean suc = matchaer.matches();
System.out.println(suc);
System.out.println(pattern.flags());
}


private static void regex001() {
Pattern p = Pattern.compile("[a-zA-Z1-9]*@[a-zA-Z1-9]*.[a-zA-Z]*");
Matcher m = p.matcher("zhjn163@qq.com");
boolean flag = m.matches();
System.out.println(flag);
}


private static void string002() {
String str = "中国";
System.out.println(str.charAt(0));
System.out.println(str.length());
char[] chs = new char[str.length()];
str.getChars(0, str.length(), chs, 0);
System.out.println(Arrays.toString(chs));
System.out.println(Arrays.toString(str.getBytes()));
}


private static void string001() throws UnsupportedEncodingException {
byte[] bytes = "中国".getBytes();
System.out.println(Arrays.toString(bytes));
String str3 = new String(bytes , "utf-8");
System.out.println(str3);
String str4 = new String(bytes , "gb2312");
System.out.println(str4);
String str5 = new String(bytes , "gbk");
System.out.println(str5);

System.out.println(str3.contentEquals(str4));
System.out.println(str3.equals(str4));

System.out.println(str3.contentEquals(str5));
System.out.println(str3.equals(str5));

System.out.println(str4.contentEquals(str5));
System.out.println(str4.equals(str5));

System.out.println(str3.compareTo(str4));
System.out.println(str3.compareTo(str4));

System.out.println(str3.compareTo(str5));
System.out.println(str3.compareTo(str5));

System.out.println(str4.compareTo(str5));
System.out.println(str4.compareTo(str5));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值