Php HTML标记上的简单正则表达式

Php HTML标记上的简单正则表达式,php,regex,Php,Regex,问题一: preg_match("#<div id=\"post_message_[a-zA-Z0-9_]*\">(.*?)</tr> <tr>#", $page, $fetchedContent); 2011年10月19日,下午4:49 注意:如上所示,上面的代码段可能具有不稳定的间距 您希望它也匹配换行符。通常不会这样做。这需要#s修饰符,基本上: preg_match('#</a>(.*?)</td> <td cla

问题一:

preg_match("#<div id=\"post_message_[a-zA-Z0-9_]*\">(.*?)</tr> <tr>#", $page, $fetchedContent);

2011年10月19日,下午4:49
注意:如上所示,上面的代码段可能具有不稳定的间距

您希望它也匹配换行符。
通常不会这样做。这需要
#s
修饰符,基本上:

  preg_match('#</a>(.*?)</td> <td class="thread"#s', ...
preg_匹配('#(.*)问题1
您还需要使用
s
标志使
匹配换行符:

preg_match("#</a>(.*?)</td> <td class=\"thread\"#s", $page, $fetchContent);
编辑-此日期正则表达式将稍微快一点(两边都添加了边界):

对于这两种情况,日期在
$results[1]
中,时间在
$results[2]

问题2 再次使用
s
标志,要在
之间使用不同的空格,请使用
*

preg_match("#<div id=\"post_message_[a-zA-Z0-9_]*\">(.*?)</tr> *<tr>#s", $page, $fetchedContent);
preg#u match(#(.*?*#s“,$page,$fetchedContent);

如果你想在
之间设置换行符,那么就改为
\s*
吧。问题1也是一样。

我要选择你的答案。日期被提取了,但时间没有提取。有什么想法吗?我试图解决,但我无法…用第二个正则表达式,它在第一个捕获组中捕获日期,在第二个捕获组中捕获时间…或者是的,(“#([0123]?[0-9]-(?:0?[1-9]| 1[012])-(?:[0-9]{4}”),((?:0[0-9]| 1[012]:[0-5][0-9]?[AP]M],…)奇怪…它对我不起作用…无论如何,感谢您的资源,稍后会检查它。起作用了…我在我本应该选择Element0时选择了数组中的元素1。
preg_match("#</a>(.*?)</td> <td class=\"thread\"#s", $page, $fetchContent);
preg_match("#([0123]?[0-9]-(?:0?[1-9]|1[012])-(?:[0-9]{4})),? ?((?:0[0-9]|1[012]):[0-5][0-9] ?[AP]M)#",...)
preg_match("#\\b([0123]?[0-9]-(?:0?[1-9]|1[012])-(?:[0-9]{4}))[, ]{1,3}((?:0[0-9]|1[012]):[0-5][0-9] ?[AP]M)\\b#",...)
preg_match("#<div id=\"post_message_[a-zA-Z0-9_]*\">(.*?)</tr> *<tr>#s", $page, $fetchedContent);