精通正则表达式第1讲(源码)

Posted yasepix

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了精通正则表达式第1讲(源码)相关的知识,希望对你有一定的参考价值。

[D:\a\精通正则表达式\精通正则表达式第1讲(源码)\src\net\itcast\course\regex\basic\lesson1\FormalExpressShow.java]
package net.itcast.course.regex.basic.lesson1;

public class FormalExpressShow {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        String regex1 = "\\d+";
        
        String str1 = "0123456";
        
        if(str1.matches(regex1)) {
            System.out.println("\"" + str1 + "\" can be matched with regex \"" + regex1 + "\"");
        }
        
        String regex2 = "sep[ea]r[ea]te";
        
        String str2 = "seperate";
        
        if(str2.matches(regex2)) {
            System.out.println("\"" + str2 + "\" can be matched with regex \"" + regex2 + "\"");
        }
        

    }

}

[D:\a\精通正则表达式\精通正则表达式第1讲(源码)\src\net\itcast\course\regex\basic\lesson1\ModeExample.java]
package net.itcast.course.regex.basic.lesson1;

public class ModeExample {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String emailRegex = Regexes.USERNAME_REGEX + "@"
                + Regexes.HOSTNAME_REGEX;

        String email = "[email protected]";

        if (email.matches(emailRegex)) {
            System.out.println("\"" + email + "\" can be matched");
        }

        else {
            System.out.println("\"" + email + "\" can not  be matched");
        }

    }

}

[D:\a\精通正则表达式\精通正则表达式第1讲(源码)\src\net\itcast\course\regex\basic\lesson1\RegexDupWordFinder.java]
package net.itcast.course.regex.basic.lesson1;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexDupWordFinder {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] sentences = new String[] { "This is a normal sentence",
                "I carelessly typed duplicate word word",
                "This sentence contain no duplicate word words", };

        for (String sentence : sentences) {
            System.out.println("Sentence is:\t" + sentence);
            if (containDupWord(sentence)) {
                System.out.println("Duplicate word found!");
                System.out.println("After processing:"
                        + removeDupWords(sentence));
            }
            System.out.println("");
        }

    }

    public static boolean containDupWord(String s) {
        String regex = Regexes.DUP_WORD_REGEX;
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(s);
        if (m.find()) {
            return true;
        } else {
            return false;
        }
    }

    public static String removeDupWords(String s) {
        String regex = Regexes.DUP_WORD_REGEX;
        return s.replaceAll(regex, "$1");
    }

}

[D:\a\精通正则表达式\精通正则表达式第1讲(源码)\src\net\itcast\course\regex\basic\lesson1\RegexEmailValidate.java]
package net.itcast.course.regex.basic.lesson1;

public class RegexEmailValidate {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] emails = new String[] { "[email protected]",
                "[email protected]", "[email protected]",
                "[email protected]", "[email protected]",
                "webmaster.itcast." };

        for (String email : emails) {

            System.out.println("Validating:\t" + email);

            if (isValideEmail(email)) {
                System.out.println("[" + email + "] is valid email");
            }

            System.out.println("");
        }

    }

    private static boolean isValideEmail(String s) {
        return s.matches(Regexes.EMAIL_REGEX);
    }

}

[D:\a\精通正则表达式\精通正则表达式第1讲(源码)\src\net\itcast\course\regex\basic\lesson1\Regexes.java]
package net.itcast.course.regex.basic.lesson1;

public class Regexes {
    
    
    public static final String OCT_NUM_REGEX = "\\d+";
    
    public static final String HEX_NUM_REGEX = "(?i)[0-9a-f]+";
    
    public static final String DUP_WORD_REGEX = "(?<=\\b)(\\w+)\\s+\\1(?=\\b)";
    
    public static final String EMAIL_REGEX = "(?i)(?<=\\b)[a-z0-9][-a-z0-9_.]+[a-z0-9]@([a-z0-9][-a-z0-9]+\\.)+[a-z]{2,4}(?=\\b)";
    
    public static final String HREF_LINK_REGEX = "(?i)<a\\s+href\\s*=\\s*[‘\"]?([^‘\"\\s>]+)[‘\"\\s>]";
    
    public static final String NUMBER_BEAUTIFY_REGEX = "(?<=\\d)(?=((\\d{3})+\\b))";
    
    public static final String USERNAME_REGEX = "(?<=\\b)[a-z0-9][-a-z0-9_.]+[a-z0-9]";
    
    public static final String HOSTNAME_REGEX = "([a-z0-9][-a-z0-9]+\\.)+[a-z]{2,4}(?=\\b)";

}

[D:\a\精通正则表达式\精通正则表达式第1讲(源码)\src\net\itcast\course\regex\basic\lesson1\RegexExtractor.java]
package net.itcast.course.regex.basic.lesson1;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexExtractor {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        String htmlCode = "<html>\n" +
        "<a href=\"www.itcast.net\">ITCast</a>\n" +
        "<a href=‘http://www.itcast.net/portal/teachers‘>ITCast</a>\n" +
        "<a href=http://www.itcast.net/portal>ITCast</a>\n" +
        "<a href  = http://www.itcast.net/portal/courses/>ITCast</a>\n" +
        "<a href    =\"http://www.itcast.net/help\">ITCast</a>\n" +
        "[email protected]\n" +
        "[email protected]\n" +
        "[email protected]\n"
        + "</html>";
        
        System.out.println("Checking email");
        for(String email:extractEmail(htmlCode)) {
            System.out.println("Email:\t" + email);
        }
        System.out.println("");

        System.out.println("Checking link");
        for(String link:extractLink(htmlCode)) {
            System.out.println("Link:\t" + link);
        }
        System.out.println("");
    }

    public static List<String> extractEmail(String s) {

        List<String> result = new ArrayList<String>();

        Pattern p = Pattern.compile(Regexes.EMAIL_REGEX);
        Matcher m = p.matcher(s);

        while (m.find()) {
            result.add(m.group());
        }

        return result;
    }

    public static List<String> extractLink(String s) {
        List<String> result = new ArrayList<String>();

        Pattern p = Pattern.compile(Regexes.HREF_LINK_REGEX);
        Matcher m = p.matcher(s);

        while (m.find()) {
            result.add(m.group(1));
        }

        return result;
    }

}

[D:\a\精通正则表达式\精通正则表达式第1讲(源码)\src\net\itcast\course\regex\basic\lesson1\RegexNumBeautifier.java]
package net.itcast.course.regex.basic.lesson1;

public class RegexNumBeautifier {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] numbers = new String[] { "1234567890", "123456", "123" };

        for (String number : numbers) {
            System.out.println(number + "\n" + beautifyNumber(number) + "\n");
        }

    }
    
    
    
    
    
    
    
    
    

    public static String beautifyNumber(String s) {
        return s.replaceAll(Regexes.NUMBER_BEAUTIFY_REGEX, ",");
    }

}

[D:\a\精通正则表达式\精通正则表达式第1讲(源码)\src\net\itcast\course\regex\basic\lesson1\RegexNumberValidate.java]
package net.itcast.course.regex.basic.lesson1;

public class RegexNumberValidate {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        String[] values = new String[] {"10", "32768", "99999", "ati", "90AF", "ffff"};
        
        for (String value : values) {
            
            System.out.println("Validating value:\t" + value);
            
            if(isOctNumber(value)) {
                System.out.println("[" + value + "] is valid Oct number");
            }
            
            if(isHexNumber(value)) {
                System.out.println("[" + value + "] is valid Hex number");
            }
            
            System.out.println("");
        }

    }
    
    public static boolean isOctNumber(String s) {
        String validateRegex = Regexes.OCT_NUM_REGEX;
        return s.matches(validateRegex);
    }
    
    public static boolean isHexNumber(String s) {
        String validateRegex = Regexes.HEX_NUM_REGEX;
        return s.matches(validateRegex);
    }

}

[D:\a\精通正则表达式\精通正则表达式第1讲(源码)\src\net\itcast\course\regex\basic\lesson1\TraditionalNumberValidate.java]
package net.itcast.course.regex.basic.lesson1;

public class TraditionalNumberValidate {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String[] values = new String[] { "10", "32768", "99999", "ati", "90AF",
                "ffff" };

        for (String value : values) {

            System.out.println("Validating value:\t" + value);

            if (isOctNumber(value)) {
                System.out.println("[" + value + "] is valid Oct number");
            }

            if (isHexNumber(value)) {
                System.out.println("[" + value + "] is valid Hex number");
            }

            System.out.println("");
        }

    }

    public static boolean isOctNumber(String s) {
        boolean result = true;
        for (int i = 0, n = s.length(); i < n; i++) {
            char c = s.charAt(i);
            if (c == ‘0‘ || c == ‘1‘ || c == ‘2‘ || c == ‘3‘ || c == ‘4‘
                    || c == ‘5‘ || c == ‘6‘ || c == ‘7‘ || c == ‘8‘ || c == ‘9‘) {
                continue;
            } else {
                result = false;
                break;
            }
        }
        return result;
    }

    public static boolean isHexNumber(String s) {
        boolean result = true;
        for (int i = 0, n = s.length(); i < n; i++) {
            char c = s.charAt(i);
            if (c == ‘0‘ || c == ‘1‘ || c == ‘2‘ || c == ‘3‘ || c == ‘4‘
                    || c == ‘5‘ || c == ‘6‘ || c == ‘7‘ || c == ‘8‘ || c == ‘9‘
                    || c == ‘a‘ || c == ‘b‘ || c == ‘c‘ || c == ‘d‘ || c == ‘e‘
                    || c == ‘f‘ || c == ‘A‘ || c == ‘B‘ || c == ‘C‘ || c == ‘D‘
                    || c == ‘E‘ || c == ‘F‘) {
                continue;
            } else {
                result = false;
                break;
            }
        }
        return result;
    }

}

 

以上是关于精通正则表达式第1讲(源码)的主要内容,如果未能解决你的问题,请参考以下文章

Python100天精通指南 之 re正则表达式

100天精通Python—第34天:正则表达式大总结

《精通正则表达式 第3版》原文总结

值得一看的书之《精通正则表达式》

精通高级RxJava 2响应式编程思想

100天精通Python(进阶篇)——第40天:pymongo操作MongoDB数据库基础+代码实战