您的位置:首页 > 其它

正则表达式

2016-03-21 16:29 204 查看
正则表达式是用于进行文本匹配的工具,也是一个匹配的表达式。换句话来说就是记录文本规则的代码。




String 的正则表达式实现代码如下:

package cn.hncu.string1;

public class StringDemo {
public static void main(String[] args) {
//demo1();
//demo2();
//demo3();
demo4();
}
public static void demo4(){
//substring
String str ="123abcrei3223";
str.substring(3, 5);
System.out.println(str);//123abcrei3223

String str2 = str.substring(3, 5);
System.out.println(str2);//ab

//split
//String user="hncu,1234,33456";//代表:用户名,密码,验证码
//String infos[]=user.split(","); //regex ---regulation expression 正则表达式

//String user="hncu 1234   33456,yuu";//代表:用户名,密码,验证码
//String infos[]=user.split(" +|,"); //regex ---regular expression 正则表达式

//String user="hncu.1234,33456 uu";//代表:用户名,密码,验证码
//String infos[]=user.split("\\.|,| "); //第一个斜杠是java的转义,第二个斜杠是正则表达式的转义

String user="hncu|1234|33456|uu";//代表:用户名,密码,验证码
//String infos[]=user.split("\\|"); //可以采用转义来处理
user = user.replace("|", "#@%$");//也可以采用替换,以避开原信息串当中的特殊字符
String infos[]=user.split("#@%\\$");
for(String info:infos){
System.out.println(info);
}

String userName="  HNcU ";//"Hncu";//HNCU, hncu "  hncu "
//equalsIgnoreCase( ) 和 compareToIgnoreCase( )
//toUpperCase( )、toLowerCase( )
//trim();
//能够把上面所有输入都看成是合法的“后台验证”
if(userName!=null && userName.trim().toLowerCase().equals("hncu")){
System.out.println("OKOK...");
}

}

public static void demo3(){
//1、字符串比较大小
String s1 = "1234中国";//49
String s2="hncuJava";//97+'h'-'a'
//System.out.println(s1>s2);//ERROR:String无法用“>”来比大小
System.out.println( s1.compareTo(s2) );

//2、子串位置查找
String s3 ="dssd2332jjdsk";
String s4 ="32j";
System.out.println(s3.indexOf(s4));

//3、查找姓名中带“飞”字的人,查找姓“张”的人。
String names[]={"张飞","李四","黄飞鸿","张三","岳飞","黄飞虎"};
for(String nm: names){
if(nm.indexOf("飞")>=0){
System.out.println(nm);
}
}
System.out.println("----");
for(String nm: names){
if(nm.indexOf("张")==0){
System.out.println(nm);
}
}

//4、查找扩展名为“.java”的文件名。
String fileNames[]={"aa.java","1.c","aa.bat","aa.java.c","rr.doc.java","黄飞虎"};
String ext=".java";
for(String name:fileNames){
if(name.endsWith(ext)){
System.out.println(name);
}
}

}

public static void demo2(){
String s = "1234567";
System.out.println( s.length() ); //7
System.out.println( s.charAt(3) ); //4
System.out.println( s.getBytes().length );//7
System.out.println("----------");

s="1234567中国";
System.out.println( s.length() ); //9
System.out.println( s.charAt(7) ); //中
System.out.println( s.getBytes().length );//GBK:11  UTF-8:13

//判断某段字符是否含有汉字(假定只有中英文字符)--个数
s="1234567jkjkd字符串中没sww23";
if(s.getBytes().length==s.length()){
System.out.println("该段字符串中没有汉字");
}else{
System.out.println("该段字符串中有汉字");
}
//int num = (s.getBytes().length-s.length());//GBK
int num = (s.getBytes().length-s.length())/2;//UTF-8
System.out.println("中文字符个数:"+num);

//分别输出一个字符串中的中文字符、英文字母、数字
char cs[] = s.toCharArray();
for(char c:cs){
if((""+c).getBytes().length >1 ){
System.out.println("汉字:"+c);
}else if(c>='a'&&c<='z' || c>='A'&&c<='Z'){
System.out.println("英文字母:"+c);
}else if(c>='0'&&c<='9'){
System.out.println("数字:"+c);
}
}

}

public static void demo1(){
//String str="aaa32dsd32";
String str=new String("aaa32dsd32");
//aa(str);
System.out.println(str);

String str2 = "hncuJava";

//返回的字符串才是替换过的,原字符串其实并没有变化
String s = str2.replace("hncu", "HNCU");
System.out.println(str2+","+s);
}

public static void aa(String str){
System.out.println("222:"+str.replace("a", "b") );
}
}


在正则表达式当中,有两个类:1 Pattern类:Java中专门进行正则表达式编译的类。(一个该类的实例即代表正则表达式本身) 2Matcher类:通过解释Pattern,对字符序列执行匹配操作的引擎。(用来判断是否匹配的)

用法:1调用Pattern的compile方法匹配规则进行编译。2将待验证的串当做pattern的matches方法参数。返回得到一个matcher对象。3调用matcher对象的matches方法返回一个布尔值,即我们所需要的验证结果


正则表达式的一些常用的元字符:



下面是结合StringBuffer写的一个实例:

代码实现如下:

package cn.hncu.regx;

public class regxDemo1 {
public static void main(String[] args) {
String str="0737-2496541";
String reg="0\\d{3}-\\d{7,8}";
boolean flag=str.matches(reg);
System.out.println(flag);
changeStr(str);
System.out.println(str);
System.out.println("---------------");
StringBuffer strBuff=new StringBuffer("if you do not leave me,I will still with you!!");
System.out.println(strBuff);
change2(strBuff);
System.out.println(strBuff.toString());
}

private static void change2(StringBuffer strBuff) {
strBuff.insert(23, "(你若不离不弃,我便生死相依) ");
}

private static String changeStr(String str) {
str=str+"1";
return str;
}

}


package cn.hncu.regx;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class regxDemo2 {
public static void main(String[] args) {
String str="0737-24696541";
String regx="0\\d{3}-\\d{5,8}";
//		int comments = Pattern.COMMENTS;
Pattern p=Pattern.compile(regx);
Matcher m=p.matcher(str);
boolean flag=m.matches();
System.out.println(flag);
System.out.println("**************");

//下面这段我也不知道干嘛的。。QUQ
StringBuffer sb=new StringBuffer("love");
String replacement="love";
Pattern p2=Pattern.compile(replacement);
Matcher m1=p2.matcher(sb);
boolean isFlag=m1.matches();
System.out.println(isFlag);
System.out.println("************");
StringBuffer buff2=m1.appendTail(sb);
System.out.println(buff2.toString());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: