《精通正则表达式》第2章Java例子

Text-to-HTML转换例子(76页)

package zcw.com.lib_regex.master;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zcw on 2019/4/28.<br><br>
 */
public class P72 {
    public static void main(String[] args) {
        String filePath = "lib_regex\\text.txt";
        StringBuilder builder = new StringBuilder();
        try {
            FileReader fileReader = new FileReader(filePath);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            // 从文件一行一行读取字符串到builder,每行以\n结束
            String content;
            while ((content = bufferedReader.readLine()) != null) {
                builder.append(content + "\n");
            }

            // 关闭文件
            fileReader.close();
            bufferedReader.close();
        }
        catch (IOException e) {
            System.err.println("Open " + filePath + "failed.\n" + e.getMessage());
            System.exit(0);
        }

        transfer(builder.toString());
    }

    /**
     * 把文本文件转换成html内容。
     * @param content 需要转换的内容
     */
    private static void transfer(String content) {
        // 匹配主机号正则表达式
        String hostName = "[-a-z0-9]+(\\.[-a-z0-9]+)*\\.(com|edu|info)";

        /**
         * 下标为[3]的正则表达式与书中不同;书中为"^\s*$",用此表达式,一直未能匹配成功,所以参考了书中81页内容,在每行开头插入<p>
         */
        String[] regex = {
                "&",        // 处理&字符
                "<",        // 处理<
                ">",        // 处理>
                "^([^\\n]+)",
                "\\b(\\w[-.\\w]*@" + hostName + ")\\b",
                "\\b(http://" + hostName + "\\b(/[-a-z0-9_:@&?=+,.!/~*'%$]*(?<![.,?!]))?)"
        };

        String[] replaceText = {
                "&amp;",
                "&lt;",
                "&gt;",
                "<p>",
        };

        for(int i = 0; i < 3; i++) {
            content = content.replaceAll(regex[i], replaceText[i]);
        }

        // 划分段落
        Pattern pattern = Pattern.compile(regex[3], Pattern.MULTILINE);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            content = matcher.replaceAll(replaceText[3] + "$1");
        }

        // 把E-mail地址转换为连接形式
        pattern = Pattern.compile(regex[4], Pattern.CASE_INSENSITIVE);
        matcher = pattern.matcher(content);
        while (matcher.find()) {
            content = matcher.replaceAll("<a href=\"mailto:$1\">$1</a>");
        }

        // 把HTTP URL地址转换为连接形式
        pattern = Pattern.compile(regex[5], Pattern.CASE_INSENSITIVE);
        matcher = pattern.matcher(content);
        while (matcher.find()) {
            content = matcher.replaceAll("<a href=\"$1\">$1</a>");
        }
        System.out.println(content);

        createHtml(content);
    }

    /**
     * 创建html文件
     * @param content
     */
    private static void createHtml(String content) {
        try {
            // 创建文件
            String filePath = "lib_regex\\test.html";
            File file = new File(filePath);
            if(!file.exists()) {
                file.createNewFile();
            }

            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

            String[] html = {
                    "<!DOCTYPE html>\n",
                    "<html lang=\"en\">\n",
                    "<head>\n",
                    "<meta charset=\"UTF-8\">\n",
                    "<title>正则表达式测试</title>\n",
                    "</head>\n",
                    "<body>\n",
                    content,
                    "</body>\n",
                    "</html>\n",
            };

            // 把内容写入html文件
            for(String string : html) {
                bufferedWriter.write(string);
            }
            bufferedWriter.flush();

            fileWriter.close();
            bufferedWriter.close();
        }
        catch (IOException e) {
            System.err.println("Create html file failed.\n" + e.getMessage());
            return ;
        }
    }
}

text.txt文本内容如下:

This is a <TAG Test> sample file.
It has three lines.
That’s all
This is & test.
jfriedl@oreilly.com
http://www.baidu.com

Copy It has three lines.
Copy That’s <TAG Test> all
Copy jfriedl@oreilly.com
Copy http://www.baidu.com

代码运行结果如图所示:在这里插入图片描述
代码运行成功之后,会生成一个可直接在浏览器中打开的test.html页面,页面效果如图所示:
在这里插入图片描述


重复单词例子(77页)

package zcw.com.lib_regex.master;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;

/**
 * Created by zcw on 2019/4/29.<br><br>
 * main方法传入参数:lib_regex\\text2.txt
 */
public class TwoWord {
    public static void main(String[] args) {
        Pattern regex1 = Pattern.compile(
                "\\b" +              // 单词边界
                "([a-z]+)" +                // 匹配单词
                "((?:\\s|<[^>]+>)+)" +      // 匹配空格或者<tag>
                "(\\1\\b)",                 // \\1为反向引用,\\b为单词边界符
                Pattern.CASE_INSENSITIVE);

        /** \033[7m为控制台输出高亮起始符,\033[m为控制台高亮结束符<br />
         *  $1用于引用捕获组
         */
        String replace1 = "\033[7m$1\033[m$2\033[7m$3\033[m";

        /** 去掉所有未标记的行,如果没有\e转义符,既为未标记的行。<br>
         * {@link Pattern#MULTILINE}处理多行,对于书中所讲m模式。
         */
        Pattern regex2 = Pattern.compile("^(?:[^\\e]*\\n)+", Pattern.MULTILINE);

        /** 匹配每一行的开头 */
        Pattern regex3 = Pattern.compile("^([^\\n]+)", Pattern.MULTILINE);

        // 对于命令行的每个参数进行如下处理
        for(int i = 0; i < args.length; i++) {
            try {
                BufferedReader in = new BufferedReader(new FileReader(args[i]));
                String text;

                while ((text = Util.getPara(in)) != null) {
                    // 应用3条规则替换
                    text = regex1.matcher(text).replaceAll(replace1);
                    text = regex2.matcher(text).replaceAll("");
                    text = regex3.matcher(text).replaceAll(args[i] + ": $1");

                    // 显示结果
                    System.out.println(text);
                }
				
				in.close();
            }
            catch (IOException e) {
                System.err.println("can't read [" + args[i] + "]: " + e.getMessage());
            }
        }

        System.out.println("\033[7m一句用于测试高亮的话\033[m");
    }
}
package zcw.com.lib_regex.master;

import java.io.BufferedReader;
import java.io.IOException;

/**
 * Created by zcw on 2019/4/29.<br><br>
 */
public class Util {

    /**
     * 用于读入“一段”文本
     * @param in 字符流
     * @return 返回一段文字,直到遇到空行
     * @throws IOException
     */
    public static String getPara(BufferedReader in) throws IOException {
        StringBuffer buffer = new StringBuffer();
        String line;

        // 如果遇到空行,则结束
        while ((line = in.readLine()) != null && (buffer.length() == 0 || line.length() != 0)) {
            buffer.append(line + "\n");
        }

        return buffer.length() == 0 ? null : buffer.toString();
    }
}

例子测试文本内容如下:

This is a sample file
File three lines pattern.
It has three three lines.
That’s all all
check for doubled words (such as thisthis thisthis), a common problem with
of this chapter. If you knew thethe <afsdf> thethe specific doubled word to find (such
jfriedl@oreilly.com
http://www.baidu.com

check for doubled words (such as thisthis thisthis), a common problem with
of this chapter. If you knew thethe <afsdf> thethe specific doubled word to find (such
jfriedl@oreilly.com
http://www.baidu.com

运行结果如图所示:
在这里插入图片描述


相关资料

控制台输出高亮相关资料:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
内容简介: 随着互联网的迅速发展,几乎所有工具软件和程序语言都支持的正则表达式也变得越来越强大和易于使用。本书是讲解正则表达式的经典之作。本书主要讲解了正则表达式的特性和流派、匹配原理、优化原则、实用诀窍以及调校措施,并详细介绍了正则表达式在Perl、 Java、.NET、PHP中的用法。 本书自第1版开始着力于教会读者“以正则表达式来思考”,来让读者真正“精通正则表达式。该版对PHP的相关内容、Java1.5和Java1.6的新特性作了可观的扩充讲解。任何有机会使用正则表达式的读者都会从中获益匪浅。 本书讲解正则表达式,这种工具能够提高工作效率、让生活变得更轻松。精心调校后的正则表达式只需要十多秒就能完成以前数小时才能完成的枯燥任务。如今,正则表达式已经成为众多语言及工具——Perl、PHP、Java、Python、Ruby、MySQL、VB.NET和C#(以及.NET Framework中的任何语言)——中的标准特性,依靠它,你能以之前完全不敢设想的方式进行复杂而精巧的文本处理。 《精通正则表达式(第3版)》包含了对PHP及其正则表达式的讲解。这一版的更新也反映了其他语言的发展,深入讲解了Sun的java.util.regex,并特别提到了Java 1.4.2和Java 1.5/1.6之间的众多差异。 本书的内容: 各种语言和工具的功能比较 正则引擎的工作原理 优化(能节省大量的时间) 准确匹配期望的文本
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值