【问题标题】:Regex building Help required需要正则表达式构建帮助
【发布时间】:2015-11-22 19:18:46
【问题描述】:

我在http://regex101.com/r/iD3xT7/1 需要帮助构建包含 html 标记、重复模式等的正则表达式

我已经完成了其中的一部分,但是当我想为<a\s[^<>]*>([^<>]*)<\/a>\s 重复该模式时,repetition 会失败。就像递归。我需要完整的模式。

【问题讨论】:

标签: regex


【解决方案1】:

警告:您不应该使用正则表达式进行 HTML 解析,
正如在 SO 上已经多次说过的那样。


也就是说,您不能只重复超链接模式。

为了更清楚,您应该使用自己的正则表达式提取每种数据。 PHP 中的示例:

$html = /* choose your way to retrieve the HTML */;
$movie = array();

preg_match('/Released:.*?<td>(.+?)<\/td>/s', $html, $matches);
$movies['lucy']['released'] = $matches[1];

preg_match('/Runtime:.*?<td>(.+?)<\/td>/s', $html, $matches);
$movies['lucy']['runtime'] = $matches[1];

preg_match_all('/<a[^>]*?genre[^>]*?>(.+?)<\/a>/', $html, $matches);
$movies['lucy']['genres'] = $matches[1];

var_dump($movies);
/*
array(1) {
  ["lucy"]=>
  array(3) {
    ["released"]=>
    string(13) "July 25, 2014"
    ["runtime"]=>
    string(8) "90 mins "
    ["genres"]=>
    array(2) {
      [0]=>
      string(6) "Action"
      [1]=>
      string(6) "Sci-Fi"
    }
  }
}
*/

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-03-17
  • 2012-11-30
  • 2023-03-19
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多