美文网首页
13.2正则表达式

13.2正则表达式

作者: 云木杉 | 来源:发表于2019-11-22 16:16 被阅读0次

正则表达式一一种强大而灵活的文本处理工具。

本以后是很简单的一个表达式,却不曾想看了快整整三天时间,各种教程文档,总算摸摸索索懂的了一些,在此也警戒自己,做事情要深入进去

String

  • split()方法
    我对此方法的理解是,按照正则表达式的规则,切割字符串,比如a1b2c3d使用\d+切割的话,就是把数字切割掉剩余的字符集合。
public class RexDemo {


    public static void main(String[] args) {

//        test();

//        test2();

//        test3();

//        test4();

//        test5();

//        test6();

        test7();

    }

    public static String input =
            "As long as there is injustice, whenever a\n" +
                    "Targathian baby cries out, wherever a distress\n" +
                    "signal sounds among the stars ... We'll be there.\n" +
                    "This fine ship, and this fine crew ...\n" +
                    "Never give up! Never surrender!";

    private static class Display {
        private boolean regexPrinted = false;
        private String regex;

        Display(String regex) {
            this.regex = regex;
        }

        void display(String message) {
            if (!regexPrinted) {
                out.println(regex);
                regexPrinted = true;
            }
            out.println(message);
        }
    }

    static void examine(String s, String regex) {
        Display d = new Display(regex);
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(s);
        while (m.find())
            d.display("find() '" + m.group() +
                    "' start = " + m.start() + " end = " + m.end());
        if (m.lookingAt()) // No reset() necessary
            d.display("lookingAt() start = "
                    + m.start() + " end = " + m.end());
        if (m.matches()) // No reset() necessary
            d.display("matches() start = "
                    + m.start() + " end = " + m.end());
    }
    private static void test7() {
        for (String in : input.split("\n")) {
            out.println("input : " + in);
            for(String regex : new String[]{"\\w*ere\\w*", "\\w*ever", "T\\w+", "Never.*?!"})
                examine(in, regex);
        }

    }

    static public final String POEM =
            "Twas brillig, and the slithy toves\n" +
                    "Did gyre and gimble in the wabe.\n" +
                    "All mimsy were the borogoves,\n" +
                    "And the mome raths outgrabe.\n\n" +
                    "Beware the Jabberwock, my son,\n" +
                    "The jaws that bite, the claws that catch.\n" +
                    "Beware the Jubjub bird, and shun\n" +
                    "The frumious Bandersnatch.";

    private static void test6() {

        Matcher m = // (字符)空格((字符)空格(字符))
                Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$")
                        .matcher(POEM);
        while (m.find()) {
            out.println(m.group());
        }

//        while (m.find()) {
////            for (int j = 0; j <= m.groupCount(); j++)
//                System.out.println("[" + m.group(1) + "]");
//            System.out.println();
//        }
    }

    private static void test5() {
        String str = "Everying is full of the linnet wings";

        Matcher matcher = Pattern.compile("\\w+").matcher(str);

        while (matcher.find()) {

            for (int i = 0; i <= matcher.groupCount(); i++) {
                out.println(" " + matcher.group());
            }

        }

    }

    private static String[] args = {"abcabccabcdefabc", "abc+", "(abc)+", "(abc){2,}"};

    private static void test4() {

//        out.println("Input :\"" + args[0] + "\"");

        for (String arg : args) {
//            out.println("Regular expression : \"" + arg + "\"");
            out.println("--------------------------------------");
            Pattern p = Pattern.compile(arg);
            Matcher m = p.matcher(args[0]);
            while (m.find()) {
                out.println("Match \"" + m.group() + "\" at positions " +
                        m.start() + " - " + (m.end() - 1));
            }
        }

    }

    private static void test3() {
        Pattern mp = Pattern.compile("\\d+");

        String[] str = mp.split("我的QQ是:456456我的电话是:0532214我的邮箱是:aaa@aaa.com");
        String[] split = "我的QQ是:456456我的电话是:0532214我的邮箱是:aaa@aaa.com".split("\\d+");
//        out.println(split[0] + " " + split[1] + " " + split[2]);
//        boolean matches = Pattern.matches("\\d+", "344e44");
//        out.println(matches);

        Matcher matcher = mp.matcher("helloworld344");
        Boolean pattern = matcher.lookingAt();

        Matcher matcher1 = mp.matcher("344helloworld");
        Boolean pattern1 = matcher1.lookingAt();

//        out.println(pattern + " " + pattern1);

    }

    private static void test2() {


        final String regex = "(\\w+)@(\\w+)\\.(\\w+)";
        final String string = "somebody@163.com\n"
                + "somebo@163.com\n"
                + "some@163.com";

        final String subst = "*@$1.$2";

        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);

// The substituted value will be contained in the result variable
        final String result = matcher.replaceAll(subst);

//        System.out.println("替换结果: " + result);

        String reg = "\\(?0\\d{2}[) -]?\\d{8}";
        String rex = "\\(?0\\d{2}[) -]?\\d{8}";


        out.println("(010)55859762".matches(reg));
    }

    private static void test() {
//        out.println("-1234".matches("-?\\d+"));

        String reg = "^[a-z0-9_-]{3,15}$";

//        out.println("12a".matches(reg));
//        out.println("1a".matches(reg));

        String reg2 = "[1-9][0-9]*";
//        out.println("5000".matches(reg2));

        String str = "colors";
        String str2 = "colours";
        String str3 = "colour";
        String reg3 = "colou?rs";

//        out.println(str.matches(reg3));
//        out.println(str2.matches(reg3));
//        out.println(str3.matches(reg3));
        String reg4 = "/d+/d{10}";
//        out.println("15700165675".matches(reg3));

        // runoo+b,可以匹配 runoob、runooob、runoooooob 等,+ 号代表前面的字符必须至少出现一次(1次或多次)。
        String reg5 = "runao\\*b";
//        out.println("runao*b".matches(reg5));
//        out.println("runaob".matches(reg5));
//        out.println("runaooob".matches(reg5));

        String reg6 = "1[0-9]{10}";
//        out.println("15700166565".matches(reg6));

        String reg7 = "<.*?>";
//        out.println("<h1>RUNOOB-菜鸟教程</h1>".matches(reg7));
        String Str = "Hello , World .";
        String pattern = "(\\w)(\\s+)([.,])";
        /**
         *  String Str = "2008-12-31";
         *  String pattern = "(\\d{4})-(\\d{2}-(\\d\\d))";
         */
        // $0 匹配 `(\w)(\s+)([.,])` 结果为 `o空格,` 和 `d空格.`
        System.out.println(Str.replaceAll(pattern, "$0")); // Hello , World .
        // $1 匹配 `(\w)` 结果为 `o` 和 `d`
        System.out.println(Str.replaceAll(pattern, "$1")); // Hello World

        // $2 匹配 `(\s+)` 结果为 `空格` 和 `空格`
        System.out.println(Str.replaceAll(pattern, "$2")); // Hell  Worl

        // $3 匹配 `([.,])` 结果为 `,` 和 `.`
        System.out.println(Str.replaceAll(pattern, "$3")); // Hell, Worl.


    }

    public static String getId(String url) {
        Pattern pattern = Pattern.compile("^https?://.+[:\\d+]?+/p/(\\d+)\\??.+$");
        Matcher matcher = pattern.matcher(url);
        if (!matcher.find()) {
            return null;
        }
        return matcher.group(1);
    }

相关文章

  • 13.2正则表达式

    正则表达式一一种强大而灵活的文本处理工具。 本以后是很简单的一个表达式,却不曾想看了快整整三天时间,各种教程文档,...

  • 13.2

    ❤️ 国学爱 爱国学 ❤️ 传统文化每日分享 ❤️《论语》十三.子路篇 【原文】 13.2 仲弓为季氏宰,问政。子...

  • 13.2 MediaPlayer

    简介 Androd多媒体框架中的一个重要组件,通过该类,我们可以以最小的步骤来获取,解码 和播放音视频。 常用方法...

  • 论语13.2

    13.2仲弓为季氏宰,问政,子曰:“先有司①,赦小过,举贤才。” 曰:“焉知贤才而举之?”子曰:“举尔所知。尔所不...

  • Xcode真机调试包 13.3

    20191216更新: 下载13.2的真机调试包(这个13.2的真机调试包是从Xcode11.3中提取的),解压缩...

  • 每周一课:L13 Fibonacci numbers(13.2)

    P13.2 Ladder Count the number of different ways of climb...

  • 我的化学老师

    文/Iris “当我们需要用试管量取13.2毫升的xx液体我们眼睛看哪?” “看13.2毫升刻度。”我的回答脱口而...

  • 13.2 并发执行

    当我们有成千上万个自动化测试用例时,为了提高测试用例的执行速度,往往我们需要对测试用例进行并发执行。并发执行的方式...

  • iOS 13.2 frameworks

  • 【崔律100天精时力训练营·学习日志·DAY83】

    #崔律100天精时力训练营13.2# 这是2019年12月3日<崔律·100天精时力训练营>之<四化·13.2 流...

网友评论

      本文标题:13.2正则表达式

      本文链接:https://www.haomeiwen.com/subject/eezfwctx.html