精通正则表达式第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[] { "w[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;
    }

}
时间: 2024-12-20 19:12:54

精通正则表达式第1讲(源码)的相关文章

Java入门到精通——框架篇之Spring源码分析Spring两大核心类

一.Spring核心类概述. Spring里面有两个最核心的类这是Spring实现最重要的部分. 1.DefaultListableBeanFactory 这个类位于Beans项目下的org.springframework.beans.factory.support包下. XmlBeanFactory(位于org.springframework.beans.factory.xml包)继承自DefaultListableBeanFactory,而DefaultListableBeanFactory

(升级版)Spark从入门到精通(Scala编程、案例实战、高级特性、Spark内核源码剖析、Hadoop高端)

本课程主要讲解目前大数据领域最热门.最火爆.最有前景的技术——Spark.在本课程中,会从浅入深,基于大量案例实战,深度剖析和讲解Spark,并且会包含完全从企业真实复杂业务需求中抽取出的案例实战.课程会涵盖Scala编程详解.Spark核心编程.Spark SQL和Spark Streaming.Spark内核以及源码剖析.性能调优.企业级案例实战等部分.完全从零起步,让学员可以一站式精通Spark企业级大数据开发,提升自己的职场竞争力,实现更好的升职或者跳槽,或者从j2ee等传统软件开发工程

2012 PHP热门资料64个+经典源码50个——下载目录 :

完整附件0豆下载:http://down.51cto.com/data/419216 附件部分预览: PHP精彩应用实例程序源码集锦 http://down.51cto.com/zt/39 无师自通:PHP自学必备资料 http://down.51cto.com/zt/249 主流PHP框架学习手册 http://down.51cto.com/zt/320 精品web开发整站源码(PHP.ASP等) http://down.51cto.com/zt/271 PHP视频教程及课件(共102讲) h

spring基于注解和配置源码解读

我们先来建立一个maven项目,引入spring文件,不爱弄的在文章最下面有代码地址可以去下载.先看,后面自己下载代码自己去尝试.先给你们吧,边尝试边看吧. 一.IOC容器注册组件的方式 1. 基础XML注入Bean 是不是超级简单的,我们由浅入深一点点来. 2. 基于注解的方式来配置 我们通过方法名就可以直接得到我们的对象了,默认就是按照方法来装配.也可以通过@Bean(value="newName") 来指定装配的名字. 3. 按照包扫描的方式装配(重点),使用@Component

Mysql源码目录结构

Programs for handling SQL commands. The "core" of MySQL. These are the .c and .cc files in the sql directory: derror.cc --- read language-dependent message file des_key_file.cc --- load DES keys from plaintext file discover.cc --- Functions for

jQuery源码 框架分析

每一个框架都有一个核心,所有的结构都是基于这个核心之上,结构建立好了之后,剩下的就是功能的堆砌. jQuery的核心就是从HTML文档中匹配元素并对其操作. 就跟一座大楼一样,让我们一步一步了解这座大厦的基石和结构. 1.构造函数 2.链式语法 3.选择器 4.扩展性  一.构造函数 我们知道类是面向对象编程的一个重要概念,它是对事物的最高抽象,它是一个模型.通过实例化一个类,我们可以创建一个实例. javascript本身没有类的概念,只有原型prototype,prototype是什么呢?它

UEditor之基于Java图片上传前后端源码研究

开心一笑 一定要快乐学习,所以学习之前先看个笑话: 刚来北京,租了一个小房,一楼,上淘宝买衣服,选了付钱了联系卖家:"我已付款,请发货."谁知那货直接说:"我看到你地址了,自己上楼来拿吧!我就在你楼上." 拿你妹,老子付了邮费的...送下来. 提出问题 Ueditor前后端源码的学习和简单的研究??? 解决问题 前提: 假如你已经看了我的前一篇文章,这一点很重要的啊,当然不看也可以,因为你已经是一个高手,就像我一样,哈哈: 假如你已经安装tomcat服务器: 假如你

Ueditor之前后端源码的学习和简单的研究

这是我的项目目录 这里写图片描述 1.从访问路径http://localhost:8081/Test/_examples/simpleDemo.html,我们主要是要看看,富文本框被加载出来之前,会调用哪些代码, 不卖关子,很明显,会调用后端的controller.jsp代码,因为我们已经在ueditor.config配置了: // 服务器统一请求接口路径 , serverUrl: URL + "jsp/controller.js 看看controller.jsp代码,上一篇文章我们已经讲了,要

Nginx源码分析(1)

Nginx是一个高性能的HTTP服务器,结合Nginx开发从入门到精通一书和Nginx源码学习服务器的高并发处理. 服务器的架构大同小异,而性能的差异主要来自对数据的处理方式上,也即进程模型和事件模型上.一个客户端请求的数据如何进行接受.存储.解析.返回是服务器做的最主要的工作. 最基础的数据结构 C++之所以不被用来写网站是因为,网站的核心是**处理字符串**.而C++的字符串处理实在比较弱,还要不断惦记GC和内存泄漏. 而Nginx首先封装了一下字符串的数据结构和API.定义位于nginx/