正则表达式JAVA254-262

来源:互联网 发布:b2b平台如运营 知乎 编辑:程序博客网 时间:2024/05/17 20:32

来源:http://www.bjsxt.com/
一、S03E254_01正则表达式01_介绍、标准字符集合、自定义字符集合

Regular Expression或Regex
这里写图片描述

这里写图片描述

这里写图片描述

RegexBuddy
这里写图片描述

语法(1)
这里写图片描述

语法(2)
这里写图片描述

语法(3)
这里写图片描述

二、S03E255_01正则表达式02_量词、贪婪和非贪婪模式

语法(4)
这里写图片描述

几个常用的非贪婪匹配Pattern    *? 重复任意次,但尽可能少重复    +? 重复1次或更多次,但尽可能少重复    ?? 重复0次或1次,但尽可能少重复    {n,m}? 重复n到m次,但尽可能少重复    {n,}? 重复n次以上,但尽可能少重复

三、S03E256_01正则表达式03_字符边界、匹配模式(单行和多行模式)

语法(5)
这里写图片描述

正则表达式的匹配模式
这里写图片描述

四、S03E257_01正则表达式04_分支结构、捕获组、非捕获组、反向引用

语法(6)
这里写图片描述

五、S03E258_01正则表达式05_预搜索、零宽断言(4个语法结构)

语法(7)
这里写图片描述

六、S03E259_01正则表达式06_电话号码、手机号码、邮箱、常用表达式

练习1
这里写图片描述

练习2
这里写图片描述

网络上的
这里写图片描述

七、S03E260_01正则表达式07_正则表达式、开发环境、文本编辑器中使用

这里写图片描述

八、S03E261_01正则表达式08_正则表达式、JAVA编程中使用、查找、替换、分割

这里写图片描述

package com.test.regularExpression;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * 测试正则表达式对象的基本用法 */public class Demo01 {    public static void main(String[] args) {                //在这个字符串:ssf9798,是否符合指定的正则表达式:\w+        //表达式对象        Pattern p = Pattern.compile("\\w+");        //创建Matcher对象//      Matcher m = p.matcher("ssf99798");  //尝试将整个字符序列与该模式匹配        Matcher m = p.matcher("ssss9&&oou433");        boolean yesorno = m.matches();        System.out.println(yesorno);    //ssf99798为true,ssss9&&oou433为false        boolean yesorno2 = m.find();    //扫描输入的序列,查找与该模式匹配的下一个子序列        //m.matches()为true,匹配完了,则m.find()从字符序列最后位置匹配,为false        //m.matches()为false,则m.find()从字符序列开始位置匹配,为true        System.out.println(yesorno2);   //当前为true        System.out.println("=====单单测试find()======================================");        Pattern p2 = Pattern.compile("\\w+");        Matcher m2 = p2.matcher("ssss9&&oou433");//      System.out.println(m2.find());  //true//      System.out.println(m2.find());  //true//      System.out.println(m2.find());  //false        while(m2.find()){            //group()和group(0)都是匹配整个表达式的子字符串,所以打印内容一样            //第一次打印ssss9,第二次打印oou433            System.out.println(m2.group());             System.out.println(m2.group(0));        }    }}
package com.test.regularExpression;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * 测试正则表达式对象中分组的处理 */public class Demo02 {    public static void main(String[] args) {                Pattern p = Pattern.compile("([a-z]+)([0-9]+)");    //有两个分组:([a-z]+)    与   ([0-9]+)        Matcher m = p.matcher("df43**dfdf545**fdg99");        while(m.find()){            System.out.println("====整个子序列=============");            System.out.println(m.group());            System.out.println("========子序列的分组===================");            System.out.println(m.group(1)); //([a-z]+)            System.out.println(m.group(2)); //([0-9]+)        }    }}
package com.test.regularExpression;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * 测试正则表达式对象的替换字符串操作 */public class Demo03 {    public static void main(String[] args) {        Pattern p = Pattern.compile("[0-9]");        Matcher m = p.matcher("aa33**dfd89*dfd87");        //替换        String newStr = m.replaceAll("#");        System.out.println(newStr); //aa##**dfd##*dfd##    }}
package com.test.regularExpression;import java.util.Arrays;/** * 测试正则表达式对象的分割字符串操作 */public class Demo04 {    public static void main(String[] args) {        String str = "a,b,c";        String[] arrs = str.split(",");        System.out.println(Arrays.toString(arrs));  //[a, b, c]        String str2 = "a9879b8978c9768";        String[] arrs2 = str2.split("\\d+");        System.out.println(Arrays.toString(arrs2)); //[a, b, c]    }}

九、S03E262_01正则表达式09_正则表达式、手写网络爬虫、基本原理、乱码处理

package com.test.regularExpression;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.MalformedURLException;import java.net.URL;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * 网络爬虫取链接(可以不用自己写,已有相关产品,如wget) */public class WebSpider {    public static void main(String[] args) {        String destResult = getURLContent("http://www.163.com","gbk");        List<String> result = getMatcherSubStr(destResult, "href=\"([\\w\\s./:]+?)\"");        for (String temp : result) {            System.out.println(temp);        }    }    /**     * 获得urlStr对应网页的源码内容      * @param urlStr     * @return     */    public static String getURLContent(String urlStr,String charset){        StringBuilder sb = new StringBuilder();        try {            URL url = new URL(urlStr);            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(),Charset.forName(charset)));            String temp = "";            while((temp=reader.readLine())!=null){//              System.out.println(temp);                sb.append(temp);                sb.append("\n");            }        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return sb.toString();    }    public static List<String> getMatcherSubStr(String destStr,String regexStr){        Pattern p = Pattern.compile(regexStr);  //取到的超链接的地址        Matcher m = p.matcher(destStr);        List<String> result = new ArrayList<String>();        while(m.find()){            result.add(m.group(1));        }        return result;    }}
0 0
原创粉丝点击