Java 中使用正则表达式可以提取替换复杂文本内容中指定的字符串,其中每次匹配到的字符串可以通过回调的方法处理。本文主要介绍Java中使用正则表达式提取替换匹配字符串的方法,以及相关的示例代码。

1、Java 正则表达式

参考文档:Java 正则表达式(Regex)

2、匹配标志(flag)

Pattern.CASE_INSENSITIVE:在执行搜索时,字母的大小写将被忽略。

Pattern.LITERAL:模式中的特殊字符没有任何特殊含义,在执行搜索时将被视为普通字符。

Pattern.UNICODE_CASE:将它与CASE_INSENSITIVE标志一起使用,那么它会对Unicode字符进行大小写不敏感的匹配。默认情况下,大小写不明感的匹配只适用于US-ASCII字符集。

详细介绍如下表:

标志

描述

Pattern.CANON_EQ

当且仅当两个字符的"正规分解(canonical decomposition)"

都完全相同的情况下,才认定匹配。比如用了这个标志之后,

表达式"a\u030A"会匹配"?"。默认情况下,

不考虑"规范相等性(canonical equivalence)"。

Pattern.CASE_INSENSITIVE

(?i)

默认情况下,大小写不明感的匹配只适用于US-ASCII字符集。

这个标志能让表达式忽略大小写进行匹配。

要想对Unicode字符进行大小不明感的匹配,

只要将UNICODE_CASE与这个标志合起来就行了。

Pattern.COMMENTS

(?x)

在这种模式下,匹配时会忽略(正则表达式里的)

空格字符(注:不是指表达式里的"\\s",

而是指表达式里的空格、tab、回车之类)。

注释从#开始,一直到这行结束。

可以通过嵌入式的标志来启用Unix行模式。

Pattern.DOTALL

(?s)

在这种模式下,表达式'.'可以匹配任意字符,

包括表示一行的结束符。

默认情况下,表达式'.'不匹配行的结束符。

(行结束符:Windows下\r\n

,Linux下\n,Mac下\r)

Pattern.MULTILINE

(?m)

在这种模式下,'^'和'$'分别

一行一行地

匹配一行的开始和结束。此外,'^'仍然匹配一行字符串的开始,

'$'也匹配一行字符串的结束。默认情况下,

这两个表达式仅仅匹配字符串的开始和结束。

Pattern.UNICODE_CASE

(?u)

在这个模式下,如果你还启用了CASE_INSENSITIVE标志,

那么它会对Unicode字符进行大小写不明感的匹配。

默认情况下,大小写不明感的匹配只适用于US-ASCII字符集。

Pattern.UNIX_LINES

(?d)

在这个模式下,只有'\n'才被认作一行的中止,

并且与'.','^',以及'$'进行匹配。

3、使用正则表达式提取替换字符串

Java中使用正则表达式提取html中指定内容并进行替换,代码如下,

import java.util.regex.*;

public class Main {

    public static void main(String[] args) {

        String content = "<p><strong>1、new 运算符</strong>:用于创建对象和调用构造函数。这个我们创建对象实例就比较常用了,比如:</p><pre class=\"prettyprint linenums cs\">     StringBuilder str=new  StringBuilder();\r\n\r</pre><p><strong>"
                + "2、new 修饰符</strong>:在用作修饰符时,new 关键字可以显式隐藏从基类继承的成员。简单的说,就是现在写的这个类,想写一个和基类中相同的成员,而不继承基类的。运用多态的特性时,也不会调用这个显示隐藏的方法。具体看下如下代码:"
                + "</p><pre class=\"prettyprint linenums\">using System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\n\r\nnamespace ConsoleApp2\r\n{\r\n    public class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            animal a = new animal();\r\n            a.name = \"animal\";\r\n            a.say();\r\n            cat c = new cat();\r\n            c.name = \"cat\";\r\n            c.say();\r\n            dog d = new dog();\r\n            d.name = \"dog\";\r\n            d.say();\r\n            sheep s = new sheep();\r\n            s.name = \"sheep\";\r\n            s.say();\r\n            animal a1 = new cat();\r\n            a1.say();\r\n            animal a2 = new dog();\r\n            a2.say();\r\n            animal a3 = new sheep();\r\n            a3.say();\r\n        }\r\n    }\r\n    class animal\r\n    {\r\n        public string name { get; set; }\r\n        public virtual void say()\r\n        {\r\n            Console.WriteLine(\"animal say\");\r\n        }\r\n    }\r\n    class cat : animal\r\n    {\r\n        public override void say()\r\n        {\r\n            Console.WriteLine(\"cat say\");\r\n        }\r\n    }\r\n    class dog : animal\r\n    {\r\n        public new void say()   //这个方法被显示隐藏了\r\n        {\r\n            Console.WriteLine(\"dog say\");\r\n        }\r\n    }\r\n    class sheep : animal\r\n    {\r\n    }\r\n\r\n}<br></pre><p><strong>";
        String replaced = Pattern.compile("<pre class=\"prettyprint linenums\\s*([a-z]*?)\"\\s*>(.+?)</pre>",Pattern.DOTALL|Pattern.MULTILINE)
                .matcher(content)
                .replaceAll(match -> {
                    System.out.println("------------------");
                    System.out.println(match.group(1));
                    System.out.println(match.group(2));
                    System.out.println(match.group());
                    System.out.println("------------------");
                    return "";
                });
        System.out.println(replaced);
    }
}

推荐文档

相关文档

大家感兴趣的内容

随机列表