------- android培训、java培训、期待与您交流! ----------
正则表达式:符合一定规则的表达式
作用:用于专门操作字符串
特点:用于一些特定的符号来表示一些代码操作,这样就简化书写,所以学习正则表达式就是在学习一些特殊符号的使用
好处:可以简化对字符串的复杂操作
弊端:符号定义越多,正则越长,阅读性越差。
具体操作功能:
1、匹配:String matches方法.用规则匹配整个字符串,只要有一处不符合规则,就结束。
2、切割:String split();
3、替换 String replaceAll();
4、获取:将字符串中的复合规则的子串取出
操作步骤:
(1)将正则表达式封装成对象
(2)让正则对象和要操作的字符串相关联
(3)关联后,获取正则匹配引擎
(4)通过引擎对符合规则的子串进行操作,比如取出
import java.util.regex.*;
class RegexDemo2
{
public static void main(String[] args)
{
getDemo();
}
public static void getDemo()
{
String str = "ming tian jiu yao fang jia le, da jia.";
System.out.println(str);
String reg = "\\b[a-z]{4}\\b";
//将规则封装成对象
Pattern p = Pattern.compile(reg);
//让正则对象和要作用的字符串相关联,获取匹配器对象
Matcher m = p.matcher(str);
//System.out.println(m.matches());//其实String类中的matches方法,用的就是Pattern对象来完成的
//只不过被String的方法封装后,用起来较简单。
//boolean b = m.find();//将规则作用到字符串上,并进行符合规则的子串查找。
//System.out.println(b);
//System.out.println(m.group());//用于获取匹配后的结果
//System.out.println("matches:"+m.matches());
while(m.find())
{
System.out.println(m.group());
System.out.println(m.start()+"...."+m.end());
}
}
}
下面的三个必须掌握
import java.util.*;
class RegexTest
{
public static void main(String[] args)
{
//ipSort();
checkMail();
}
/*
需求:对邮件地址进行校验
*/
public static void checkMail()
{
String mail = "abc12@sina.com.cn";
mail = "1@1.1";
String reg = "[a-zA-Z0-9——]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";//较为精确的匹配
reg = "\\w+@\\w+(\\.\\w+)+";//相对不太精确的匹配
//mail.indexOf("@"!)=-1
System.out.println(mail.matches(reg));
}
/*
需求:
将下列字符串转成:我要学编程
到底用四种功能中的哪一个呢?或者哪几个呢?
思路方式:
1、如果只想知道该字符是否对是错,使用匹配
2、想要将已有的字符串变成另一个字符串,替换
3、想要按照指定的方式将字符串变成多个字符串。切割,获取规则以外的子串
4、想要拿到符合需求的字符串子串,获取,获取符合规则的子串
*/
public static void test_1()
{
String str = "我我...我我...我要..要要..要要...学学学....学学...编编编...编程..程.程程...程...程";
/*
将已有的字符串变成另一个字符串。使用替换功能
1、可以先将.去掉
2、再将多个重复的内容变单个内容
*/
str = str.replaceAll("\\.+","");
System.out.println(str);
str = str.replaceAll("(.)\\1+","$1");
System.out.println(str);
}
/*
192.68.1.254 102.49.23.013 10.10.10 2.2.2.2 8.109.90.30
将ip地址进行地址段顺序的排序
还按照字符串自然顺序,只要让它们每一段都是3位即可
1、按照每一段需要的最多的0进行补齐,那么每一段至少保证有3位
2、将每一段只保留3位,这样,所有的ip地址都是每一段3位
*/
public static void ipSort()
{
String ip = "192.68.1.254 102.49.23.013 10.10.10 2.2.2.2 8.109.90.30";
ip = ip.replaceAll("(\\d+)","00$1");
System.out.println(ip);
ip = ip.replaceAll("0*(\\d{3})","$1");
System.out.println(ip);
String[] arr = ip.split(" ");
TreeSet<String> ts = new TreeSet<String>();
for(String s : arr)
{
ts.add(s);
}
for(String s : ts)
{
System.out.println(s.replaceAll("0*(\\d)","$1"));
}
}
}
网页爬虫(蜘蛛)
import java.io.*;
import java.util.regex.*;
import java.net.*;
class RegexTest2
{
public static void main(String[] args) throws Exception
{
getMails_1();
}
public static void getMails_1()throws Exception
{
URL url = new URL("http://192.168.1.254:8080/myweb/mail.html");
URLConnection conn = url.openConnection();
BufferedReader bufIn = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
String mailreg = "\\w+@\\w+(\\.\\w+)+";
Pattern p = Pattern.compile(mailreg);
while((line=bufIn.readLine())!=null)
{
Matcher m = p.matcher(line);
while(m.find())
{
System.out.println(m.group());
}
}
}
/*
获取指定文档中的邮件地址
使用获取功能 Pattern Matcher
*/
public static void getMails()throws Exception
{
BufferedReader bufr =
new BufferedReader(new FileReader("mail.txt"));
String line = null;
String mailreg = "\\w+@\\w+(\\.\\w+)+";
Pattern p = Pattern.compile(mailreg);
while((line=bufr.readLine())!=null)
{
Matcher m = p.matcher(line);
while(m.find())
{
System.out.println(m.group());
}
}
}
}