【问题标题】:How to make a pattern-matcher regex for html tags?如何为 html 标签制作模式匹配器正则表达式?
【发布时间】:2019-02-25 14:33:02
【问题描述】:

我正在尝试匹配模式以获取两个 html 标记之间的标记和数据。

要替换两个标签之间的数据,我想检查该模式的元素 我想制作模式正则表达式,以便我可以将其与 html 元素匹配并到达该点并替换标签之间的数据。

如果有人知道如何为下面的 html 标签创建正则表达式模式。

我的 HTML 文件是这样的:

 <div id="frame">
            <div class="content">
                <div class="messages">
                    <ul>
                        <li class="sent">
                            <img src="http://emilcarlsson.se/assets/mikeross.png" alt="" />
                            <p>####data</p>
                        </li>
                        <li class="replies">
                            <img src="http://emilcarlsson.se/assets/harveyspecter.png" alt="" />
                            <p>####data</p>
                        </li>

                    </ul>
                </div>
            </div>
        </div>

我做了什么:

 public void readWritedatatFromHtml(){
        InputStream input;
        try {

            input = getResources().openRawResource(R.raw.view);

            int size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();

            String text = new String(buffer);

            //  Pattern tags = Pattern.compile ("<div class=\"content\">+<div class=\"messages\">+<ul>");
           // Pattern tags = Pattern.compile ("<div class=\"content\">\n<div class=\"messages\">");
           // Pattern tags = Pattern.compile ("<div class=\"content\">(.*?)<ul>");


            Pattern tags = Pattern.compile ("<div class=\"messages\">.? </div>");
            Matcher m = tags.matcher(text);
            StringBuffer sb = new StringBuffer();

            while (m.find()) {
                m.appendReplacement(sb, " <ul> <li class=\"sent1\">\n" +
                        "                            <img src=\"http://emilcarlsson.se/assets/mikeross.png\" alt=\"\" />\n" +
                        "                            <p>####data</p>\n" +
                        "                        </li>");
            }

            m.appendTail(sb);
            Log.i("sb",sb.toString());

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
                    }
                    }

【问题讨论】:

    标签: android html regex


    【解决方案1】:

    在任何情况下都不要尝试使用正则表达式解析 HTML,除非您希望调用 rite 666Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn。

    使用 HTML 解析库,请参阅 this page 了解一些方法。

    【讨论】:

    • 我认为你是对的,但我认为但我的要求是这样的,这可能就是我必须这样做的原因,但你应该避免这样做以获得最佳实践。
    【解决方案2】:

    好吧,在尝试了一些模式之后,我发现这样的东西非常适合我:

     Pattern tags = Pattern.compile ("<div\\s+class=\"messages\">[\\S\\s]*?<\\/div>");
    

    正如@JGNI 建议的那样,我们应该避免这种情况,但现在如果有人有更好的选择,请指导我,这样它也可以对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      • 2015-03-31
      • 2011-12-15
      相关资源
      最近更新 更多