【问题标题】:how to transform HTML to Regex如何将 HTML 转换为正则表达式
【发布时间】:2017-05-25 06:57:39
【问题描述】:

我将从 href="download.php?id=469979" 中提取此“OutDaughtered.S02E03.A.Quint.In.Crisis.720p.TLC.WEBRip.AAC2.0.x264-BTW.torrent”

<a class="index" href="download.php?id=469979"><img src="styles/images/download.png" style="position:relative;bottom:1px;" alt="Download" border="0"> OutDaughtered.S02E03.A.Quint.In.Crisis.720p.TLC.WEBRip.AAC2.0.x264-BTW.torrent</a>

我有这个代码:

# Search for filename
my $match_filename=qr/<a class="index" href="download.php\/.*?\/.*?.torrent"><font color=.*?>(.*?).torrent<\/font><\/a> .Press/ms;
my $filename_matches=$r->match('filename', $match_filename);
if($filename_matches==0){ $r->err('Can not continue without filename, aborting!'); return 0;}
my $filename=@$filename_matches[0];
$r->err("Filename: $filename");

我有这个错误:

Failed to match filename
Can not continue without filename, aborting!

谢谢

【问题讨论】:

  • 现在说什么?你想做什么,为什么要做,想要达到什么结果?
  • 目前还不清楚您实际问的是什么。 “将 html 转换为正则表达式”是什么意思?正则表达式是 search 模式,html 是标记代码。他们没有任何共同之处。你不能将它们“转换”成彼此。
  • 也许您想要可以匹配该代码的正则表达式?请解释一下。
  • .* 将转换 any HTML、any 文本.... anything
  • 我将从“href="download.php?id=469979"”中提取这个 OutDaughtered.S02E03.A.Quint.In.Crisis.720p.TLC.WEBRip.AAC2.0.x264- BTW.torrent

标签: html regex transform


【解决方案1】:

PHP 代码:

<?php
$re = '/(?<=">\s)(?:.*)(?=<)/m';
$str = '<a class="index" href="download.php?id=469979"><img src="styles/images/download.png" style="position:relative;bottom:1px;" alt="Download" border="0"> OutDaughtered.S02E03.A.Quint.In.Crisis.720p.TLC.WEBRip.AAC2.0.x264-BTW.torrent</a>
';

preg_match_all($re, $str, $matches);

// Print the entire match result
print_r($matches);
?>

正则表达式:

/(?<=">\s)(?:.*)(?=<)/m

结果:

Array
(
    [0] => Array
        (
            [0] => OutDaughtered.S02E03.A.Quint.In.Crisis.720p.TLC.WEBRip.AAC2.0.x264-BTW.torrent
        )

)

见:https://regex101.com/r/vYGgJz/1

【讨论】:

  • 我将从这个 href="download.php?id=469979" 中提取这个 .torrent 文件
  • 它将提取没有标签的文本。
猜你喜欢
  • 2022-10-04
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-11
相关资源
最近更新 更多