---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------
正则表达式的特点
正则表达式:符合一定规则的表达式。
作用:用于专门操作字符串。
可以简化对字符串的复杂操作
示例代码如下:
/*
* 需求:对qq号码进行校验,要求5-15,0不能开头,只能是数字
*
* 代码中的两个方法都是同一个功能,但用正则表达式写的明显比正常写法简洁。
* */
public class Demo{
public static void main(String[] args){
checkQQ_1();
checkQQ_2();
}
public static void checkQQ_2(){
String qq="120044QQ00";
String regex="[1-9][0-9]{4,14}";
boolean flag=qq.matches(regex);
if(flag)
System.out.println(qq+"...is ok!");
else
System.out.println(qq+"..不合法!");
}
public static void checkQQ_1(){
String qq="123456789";
int len=qq.length();
if(len>=5&&len<=15){
if(!qq.startsWith("0")){
char[] arr=qq.toCharArray();
boolean flag=true;
for(int x=0;x<arr.length;x++){
if(!(arr[x]>='0'&&arr[x]<='9')){
flag=false;
` break;
}
}
if(flag)
System.out.println("qq:"+qq);
else
System.out.println("出现非法字符");
}
}else
System.out.println("不可以0开头");
}
}
正则表达式的功能
切割
示例代码如下:
public class Demo{
public static void main(String[] args){
String str="zhangsankkzhaosikkwangwu";
String regex="kk";
String[] ss=str.split(regex);
for(String s:ss){
System.out.println(s);
}
}
}
//output:
//zhangsan
//zhaosi
//wangwu
替换
示例代码如下:
public class Demo{
public static void main(String[] args){
String str="zhangsan::::::zhaosi!!!!!!!!wangwu";
String regex="(.)\\1+";
str=str.replaceAll(regex,"$1");
System.out.println(str);
}
}
//output:zhangsan:zhaosi!wangwu
获取
示例代码如下:
import java.util.regex.*;
class Demo{
public static void main(String[] args){
String str="ming tian jiu yao fang jia le ,da jia.";
String reg="\\b[a-z]{4}\\b";
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(str);
while(m.find()){
System.out.println(m.group());
System.out.println(m.start()+"..."+m.end());
}
}
}
1、去除重复汉字
示例代码如下:
import java.util.regex.*;
public class Demo{
public static void main(String[] args) {
String str="我我...我我...我我...要要...要要要学学学学学.......学编.编编编....编编编..程程程..程程程程";
String regex_1="\\.+";
String regex_2="(.)\\1+";
str=str.replaceAll(regex_1, "");
str=str.replaceAll(regex_2, "$1");
System.out.println(str);
}
}
//我要学编程
2、将ip地址进行ip地址段排序
示例代码如下:
import java.util.*;
public class Demo{
public static void main(String[] args) {
String str="192.168.1.234 127.0.0.1 10.0.2.1 4.3.3.3";
str=str.replaceAll("(\\d+)","00$1");
System.out.println(str);
str=str.replaceAll("0*(\\d{3})","$1");
System.out.println(str);
String[]str_arr=str.split(" ");
TreeSet<String> ts=new TreeSet<String>();
for(String s:str_arr){
ts.add(s);
System.out.println(s);
}
for(String s:ts){
s=s.replaceAll("0*(\\d+)","$1");
System.out.println(s);
}
}
}
3、对邮件地址进行校验
import java.util.*;
public class Demo{
public static void main(String[] args) {
String mail="@";
mail="1@1.1";
String reg="[a-zA-Z0-9_]+@[a-zA-Z0-9]+{\\.[a-zA-Z]+)+}";
reg="\\w+@\\w+(\\.\\w+)+";
System.out.println(mail.matches(reg));
}
}
4、网页爬虫
示例代码如下:
import java.net.*;
import java.io.*;
import java.util.*;
public class Demo{
public static void main(String[] args){
URL url=new URL("http://bbs.itheima.com/forum-206-1.html");
URLConnection rulconn=rul.openConnection();
BufferedInputStream bis=new BufferedInputStream(urlconn.getInputStream());
byte[] buf=new byte[1024];
int len=0;
String regex="[\\w]+@[\\w]+(\\.\\w+)+";
Pattern p=Pattern.compile(regex);
while((len=bis.read(buf))!=-1){
String data=new String(buf,0,len);
Matcher m=p.matcher(data);
if(m.find()){
System.out.println(m.group());
}
}
bis.close();
}
}
----------------------
ASP.Net+Android+IO开发S
、
.Net培训
、期待与您交流! ----------------------