正则表达式:符合一定规则的表达式
作用:用于专门操作字符串
需求:对QQ号码进行校验,要求:5~15 0不能开头,只能是数字
用String类中的方法进行组合完成需求代码复杂,因而采用正则表达式:
class RegexDemo
{
public static void main(String[] args)
{
checkqq_2();
}
public static void checkqq_2()
{
String qq = "15462416";
String regex = "[1-9][0-9]{4,14}";
boolean flag = qq.matches(regex);
if(flag)
{
System.out.println("qq:"+qq);
}
else
{
System.out.println("有非法字符");
}
}
正则表达式:符合一定规则的表达式。
作用:用于专门操作字符串。
特点:用于一些特定的符号来表示一些代码操作。这样就简化书写。
所以学习正则表达式,就是在学习一些特殊符号的使用。
好处:可以简化对字符串的复杂操作。
弊端:符号定义越多,正则越长,阅读性越差。
具体操作功能:
1,匹配:String matches方法。用规则匹配整个字符串,只要有一处不符合规则,就匹配结束,返回false。
程序实例:
public class RegexDemo {
public static void main(String[] args) {
check_QQ("355");
}
public static void check_QQ(String QQ){
String regex = "[1-9][0-9]{4,15}";
boolean flag = QQ.matches(regex);
if(flag){
System.out.println("QQ"+QQ);
}
else{
System.out.println("有非法字符!");
}
}
//匹配手机号段只有 13xxx 15xxx 18xxxx
public static void check_Tel(String Tel){
String regex = "1[358]\\d{9}";
boolean flag = Tel.matches(regex);
if(flag){
System.out.println("Tel"+Tel);
}
else{
System.out.println("手机号不匹配!");
}
}
}
2,切割:String split();
public class splitDemo {
public static void main(String[] args) {
//split("ss kkwwjjj wwxxx"," +");//按照多个空格来切割
//split("erkktyqqquizzzzzo","(.)\\1+");//按照叠词完成切割。为了可以让规则的结果被重用
//可以将规则封装成一个组。用()完成。组的出现都有编号。
//从1开始。 想要使用已有的组可以通过 \n(n就是组的编号)
//split("zhangsan.lisi.wangwu","\\.");
split("c:\\abc\\a.txt","\\\\");
}
public static void split(String str,String regex){
String[] arrs = str.split(regex);
for(String arr : arrs ){
System.out.println(arr);
}
}
}
3,替换:String replaceAll(regex,str);如果regex中有定义组,可以在第二参数中通过$符号获取正则表达式中的已有的组。
public class ReplaceAllDemo {
public static void main(String[] args) {
//将字符串中的数组替换成#。
String str = "wer1389980000ty1234564uiod234345675f";
replaceAll(str,"\\d{5,}","#");
//将叠词替换成$. //将重叠的字符替换成单个字母。zzzz->z
String str1 = "erkktyqqquizzzzzo";
replaceAll(str1,"(.)\\1+","$1");//erktyquizo
}
public static void replaceAll(String str,String reg,String newStr)
{
str = str.replaceAll(reg,newStr);
System.out.println(str);
}
}
4,获取:将字符串中的符合规则的子串取出。
操作步骤:
1,将正则表达式封装成对象。
2,让正则对象和要操作的字符串相关联。
3,关联后,获取正则匹配引擎。
4,通过引擎对符合规则的子串进行操作,比如取出。
Pattern和Matcher:
指定为字符串的正则表达式必须首先被编译为此类的实例。然后,可将得到的模式用于创建Matcher
对象,依照正则表达式,该对象可以与任意字符序列
匹配。执行匹配所涉及的所有状态都驻留在匹配器中,所以多个匹配器可以共享同一模式。
程序实例:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GetDemo {
/**
* @param args
*/
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 regex = "\\b[a-z]{4}\\b";
//将规则封装成对象。
//让正则对象和要作用的字符串相关联。获取匹配器对象。
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
/*System.out.println(m.matches());//其实String类中的matches方法。用的就是Pattern和Matcher对象来完成的。
//只不过被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());
}
}
}
程序:ip地址排序:
public class RegexTest2 {
/*192.168.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30
将ip地址进行地址段顺序的排序
*/
//思路:还按照字符串自然顺序,只要让它们每一段都是3位即可。
//1,按照每一段需要的最多的0进行补齐,那么每一段就会至少保证有3位。
//2,将每一段只保留3位。这样,所有的ip地址都是每一段3位。
public static void main(String[] args) {
String ip = "192.68.1.254 102.49.23.013 10.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);
TreeSet<String> tr = new TreeSet<String>();
String arr[] = ip.split(" ");
for(String s :arr){
tr.add(s);
}
for(String s : tr){
System.out.println(s.replaceAll("0*(\\d+)","$1"));
}
}
}
邮箱的匹配:
public class RegexTest3 {
public static void main(String[] args) {
String mail = "abc12@sina.com";
String regex = "[a-zA-Z0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";//较为精确的匹配。
regex = "\\w+@\\w+(\\.\\w+)+";//相对不太精确的匹配。
//mail.indexOf("@")!=-1
System.out.println(mail.matches(regex));
}
}
网页爬虫:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest4 {
public static void main(String[] args) throws IOException {
getMail_1();
}
/*获取指定文档中的邮件地址
使用获取功能,Pattern Matcher*/
public static void getMail_1() throws IOException{
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 len = null;
String mailreg = "\\w+@\\w+(\\.\\w+)+";
Pattern p = Pattern.compile(mailreg);
while((len = bufin.readLine())!=null){
Matcher m = p.matcher(len);
while(m.find()){
System.out.println(m.group());
}
}
}
}