我需要哪个正则表达式来实现这一点


Which regex expression do I need for this?

我有点困在这里了
我有这样的模式:
<a class="title" href="showthread.php?t=XXXXX" id="thread_title_XXX">DATADATA</a>
我知道在我的字符串(网页(中,我所有的数据都以这种格式存储,而它有我刚刚写的"唯一签名"。XXX的计数是动态的,可能在2到12个DIGITS之间(每个X是一个数字(
我可以写一个长表达式来找到整行,但我想提取数据,而不是整件事

我该怎么做?举个例子就好了
非常感谢。

忘记正则表达式吧,它们不是用来解析HTML之类的格式的,尤其是在已经有实际的解析器的情况下。

使用XPath:查找节点

$html = <<<EOT
<html>
Some html
<a class="title" href="showthread.php?t=XXXXX" id="thread_title_XXX">DATADATA</a>
</html>
EOT;
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//a[starts-with(@href, "showthread.php")]') as $node) {
    // ...
}

然后使用substr、strpos和parse_str:提取数据

$href = $node->getAttribute('href');
parse_str(substr($href, strpos($href, '?')+1), $query);
$t = $query['t'];
$id = $node->getAttribute('id');
$title = substr($id, strlen('thread_title_'));
$data = $node->nodeValue;
var_dump($t, $title, $data);

你得到:

string(5) "XXXXX"
string(3) "XXX"
string(8) "DATADATA"

尝试使用:

 $parsed_str = '<a class="title" href="showthread.php?t=45343" id="thread_title_XXX">DATADATA</a><a class="title" href="showthread.php?t=466666" id="thread_title_XXX">DATADATA</a> fasdfasdfsdfasd gfgfkgbc  04034kgs <fdfd> dfs</fdfa> <a class="title" href="showthread.php?t=7777" id="thread_title_XXX">DATADATA</a>';
 preg_match_all("/.*?'?t'=(['d]{2,12}).*?/", $parsed_str, $result);
 print_r($result);

您到底想做什么?获取XXXXX签名或所有链接?

试试这个-这是一个签名和数据

<?php 
$S = '<a class="title" href="showthread.php?t=1234567" id="thread_title_XXX">DATADATA</a>';
$pattern = '!<a.*href="showthread.php'?t=(.*)".* id=".*">(.*)</a>!';
echo "<pre>";
print_r(preg_match($pattern, $S, $res));
print_r($res);
echo "</pre>";
?>