正则表达式

来源:互联网 发布:矩阵迹的性质 编辑:程序博客网 时间:2024/05/22 16:52


正则表达式:

注意事项:

正则表达式中想表达一个.需要对它转义:\.
java中想表达一个\需要对它转义:\\
所以在java的正则表达式中想表达一个.为:\\.
在不需要对\转义的环境中(一些正则表达式工具、js好像不需要)也是:\.
==================================================================

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


public class RegexDemo2 {


/**
* @param args
*/
public static void main(String[] args) {


/*
* 正则表达式对字符串的常见操作:
* 1, 匹配。
* 其实使用的就是String类中的matches方法。

* 2,切割。
* 其实使用的就是String类中的split方法。 

* 3,替换。
* 其实使用的就是String类中的replaceAll()方法。

* 4,获取。
*
*/

functionDemo_4();
}

/*
* 获取 
* 将正则规则进行对象的封装。 
* Pattern p = Pattern.compile("a*b");
*  //通过正则对象的matcher方法字符串相关联。获取要对字符串操作的匹配器对象Matcher .
  * Matcher m = p.matcher("aaaaab");
  * //通过Matcher匹配器对象的方法对字符串进行操作。
  * boolean b = m.matches();


*/
public  static void functionDemo_4() {

String str = "da jia hao,ming tian bu fang jia!";

String regex = "\\b[a-z]{3}\\b";

//1,将正则封装成对象。
Pattern p = Pattern.compile(regex);
//2, 通过正则对象获取匹配器对象。 
Matcher m = p.matcher(str);

//使用Matcher对象的方法对字符串进行操作。
//既然要获取三个字母组成的单词 
//查找。 find();
System.out.println(str);
while(m.find()){
System.out.println(m.group());//获取匹配的子序列

System.out.println(m.start()+":"+m.end());
}
}


/*
* 替换 
*/
public static void functionDemo_3() {

String str = "zhangsanttttxiaoqiangmmmmmmzhaoliu";

str = str.replaceAll("(.)\\1+", "$1");

System.out.println(str);

String tel = "15800001111";//158****1111;

tel = tel.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");

System.out.println(tel);

}


/*
* 切割。

* 组:((A)(B(C))) 
*/
public static void functionDemo_2(){


String str = "zhangsanttttxiaoqiangmmmmmmzhaoliu";

String[] names = str.split("(.)\\1+");//str.split("\\.");

for(String name : names){
System.out.println(name);
}

}
/*
* 演示匹配。 
*/
public static void functionDemo_1(){


//匹配手机号码是否正确。 
String tel = "15800001111";

String regex = "1[358]\\d{9}";   

boolean b = tel.matches(regex);
System.out.println(tel+":"+b);
}



}

==========================================

import java.util.TreeSet;


public class RegexTest {


/**
* @param args
*/
public static void main(String[] args) {


/*
* 1,治疗口吃:我我...我我...我我我要...要要要要...要要要要..学学学学学...学学编编...编编编编..编..程程...程程...程程程
* 2,对ip地址排序。 
* 3,对邮件地址校验。 
*/
test_3();
}

//对邮件地址校验。
public static void test_3() {

String mail = "abc1@sina.com.cn";

String regex = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]{1,3})+";

regex = "\\w+@\\w+(\\.\\w+)+";//1@1.1



boolean b = mail.matches(regex);

System.out.println(mail+":"+b);


}
/*
* 1,治口吃。
*/
public static void test_1(){

String str = "我我...我我...我我我要...要要要要...要要要要..学学学学学...学学编编...编编编编..编..程程...程程...程程程";

//1,将字符串中.去掉。 用替换。
str = str.replaceAll("\\.+", "");
System.out.println(str);

//2,替换叠词。
str = str.replaceAll("(.)\\1+", "$1");
System.out.println(str);


}
/*
* ip地址排序。 

* 192.168.10.34 127.0.0.1 3.3.3.3  105.70.11.55
*/
public static void test_2(){

String ip_str = "192.168.10.34  127.0.0.1  3.3.3.3  105.70.11.55";


//1,为了让ip可以按照字符串顺序比较,只要让ip的每一段的位数相同。
//所以,补零,按照每一位所需做多0进行补充。每一段都加两个0.

ip_str = ip_str.replaceAll("(\\d+)", "00$1");
System.out.println(ip_str);

//然后每一段保留数字3位。
ip_str = ip_str.replaceAll("0*(\\d{3})", "$1");
System.out.println(ip_str);


//1,将ip地址切出。
String[] ips = ip_str.split(" +");

TreeSet<String> ts = new TreeSet<String>();

for(String  ip : ips){
// System.out.println(ip);
ts.add(ip);
}

for(String ip : ts){
System.out.println(ip.replaceAll("0*(\\d+)", "$1"));
}

}


}

原创粉丝点击