【问题标题】:find specific string in html source by using RegEx [duplicate]使用 RegEx 在 html 源代码中查找特定字符串 [重复]
【发布时间】:2015-06-17 07:43:54
【问题描述】:

很简单。

我只想从 unicode HTML 源代码中提取一些字符串值。

原文如下:

<div id="encompass">
    <tr class="lineonoff">
                <td class="xsmall">27</td>
                <td>DATE</td>
                <td class="left">TITLE</td>
                <td>STATUS</td>
                <td><a href="javascript:viewData(ID, '')" class="button purple small"><span>A</span></a></td>
              </tr>
              <tr class="lineonoff">
                <td class="xsmall">28</td>
                <td>DATE</td>
                <td class="left">TITLE</td>
                <td>STATUS</td>
                <td><a href="javascript:viewData(ID, '')" class="button purple small"><span>B</span></a></td>
              </tr>
              <tr class="lineonoff">
                <td class="xsmall">29</td>
                <td>DATE</td>
                <td class="left">TITLE</td>
                <td>STATUS</td>
                <td><a href="javascript:viewData(ID, '')" class="button purple small"><span>C</span></a></td>
              </tr>
</div>

我想提取 TITLE、DATE、STATUS、ID。

我尝试了许多可能的 RegEx 变体,但最后都失败了..

 final Pattern pattern = Pattern.compile(PATTERN_STRING);
Matcher matcher = pattern.matcher(result.toString());

如何提取这些值?谢谢!

【问题讨论】:

  • ...尤其是its legendary answer。简而言之,不要使用正则表达式。使用 HTML 解析器解析 HTML。在主要搜索引擎中搜索java html parser 会出现许多选项。

标签: java android html regex


【解决方案1】:

首先,你should not use a regex to parse HTML。 首选use a parser

但经过所有这些考虑,像这样肮脏的东西可能会胜任这项工作:

<tr[\s\S]*?class\="left">([^<]*)[\s\S]*?<td>([^<]*)[\s\S]*?viewData\(([^\(]*),

https://regex101.com/r/lZ6rE0/1

【讨论】:

    猜你喜欢
    • 2021-01-19
    • 1970-01-01
    • 2016-01-12
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2018-11-30
    • 2015-08-18
    相关资源
    最近更新 更多