Java工具类之Pattern和Matcher(一)

来源:互联网 发布:拼人脸五官的软件 编辑:程序博客网 时间:2024/06/07 00:16

我们在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));
}
}
原创粉丝点击