将计划文本与HTML内容匹配

时间:2017-06-19 15:12:54

标签: java regex string

我需要在工厂文本和HTML内容之间进行匹配,一旦找到匹配,我需要提取匹配的HTML内容(不更改HTML内容,因为我需要完全相同的HTML内容),我能够在许多场景中使用java regex实用程序进行匹配,但在以下场景中失败了。

以下是我用来匹配Text与HTML String

的示例代码
public static void main(String[] args) {

    String text = "A crusader for the rights of the weaker sections of the Association's (ADA's),choice as the presidential candidate is being seen as a political masterstroke.";
    String regex = "A crusader for the rights of the weaker sections of the Association's (ADA's) ".replaceAll(" ", ".*");

    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(text);
    // Check all occurrences
    while (matcher.find()) {

        System.out.print("Start index: " + matcher.start());
        System.out.print(" End index: " + matcher.end());
        System.out.println(" Found: " + matcher.group());

    }
}

在边缘情况下失败

案例1:

来源文字: = "A crusader for the rights of the weaker sections of the Association's (ADA's),choice as the presidential candidate is being seen as a political masterstroke.";

要匹配的文字 = "A crusader for the rights of the weaker sections of the Association's (ADA's)"

预期输出: “A crusader for the rights of the weaker sections of the Association's (ADA's)”

案例2:

来源文字:

“<ul>
   <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
   <li>Aliquam tincidunt mauris eu risus.</li>
   <li>Vestibulum auctor dapibus neque.</li>
see (<a href=\"https://www.webpagefx.com/web-design/html-ipsum/">HTML Content Sample </a>.)
</ul>”

要匹配的文字: “see (HTML Content Sample.)”

预期输出: “see (<a href=\"https://www.webpagefx.com/web-design/html-ipsum/">HTML Content Sample </a>.)”

案例3: 来源文字: = "Initial history includes the following:</p>\n<p>Documentation of <li>Aliquam tincidunt mauris eu risus.</li>"

要匹配的文字 = "Initial history includes the following: Documentation of"

来自匹配的预期输出: ”Initial history includes the following :</p>\n<p>Documentation of”

1 个答案:

答案 0 :(得分:0)

我最近提出了一个匹配HTML标记的正则表达式,支持引用属性和引用属性中的转义引号:它类似于<([^'">]|"([^\\"]|\\"?)+"|'([^\\']|\\'?)+')+>

我认为在保留HTML的同时在HTML中搜索纯文本的最简单方法是修改纯文本,使其忽略字边界处的标记,àla

// Usage: htmlSearch("ab cd").matcher("<b>ab</b> <i>cd</i>").matches();
public static Pattern htmlSearch(String plain) {
    // Check for tags before and after every word, number and symbol
    plain = plain.replaceAll("[A-Za-z]+|\\d+|[^\\w\\s]", 
            "``TAGS``$0``TAGS``";
    // Check for tags wherever (one or more) spaces are found
    plain = plain.replaceAll("\\s+", "((\\s|&nbsp;)+|``TAGS``)*");
    // Handle special characters
    plain = plain
            .replace("<", "(<|&lt;|&#60;)")
            .replace(">", "(>|&gt;|&#62;)")
            .replace("&", "(&|&amp;|&#38;)")
            .replace("'", "('|&apos;|&#39;)")
            .replace("\"", "(\"|&quot;|&#34;)")
            .replaceAll("[()\\\\{}\\[\\].*+]", "\\$0");
    // Insert the ``TAGS`` pattern
    String tags = "(<([^'\">]"
                + "|\"([^\\\"]|\\\"?)+"
                + "|'([^\\']|\\'?)+')+>)*";
    plain = plain.replace("``TAGS``", tags);

    return Pattern.compile(plain);
}