我有一段html代码,如下所示:
<td width="24%"><b>Something</b></td>
<td width="1%"></td>
<td width="46%" align="center">
<p><b>
needed
value</b></p>
</td>
<td width="28%" align="center">
</td>
</tr>在单词Something之后提取第一个文本节点(不是标记,而是文本)的好的regex模式是什么,我的意思是要提取
needed
value仅此而已。
我无法在php中找到一个有效的regex模式。
编辑:,我不是解析整个html文档,而是解析其中的几行,所以我想要做的就是使用Regex,而不使用解析器。
发布于 2012-10-04 17:33:32
忽略使用regex解析HTML的潜在问题,下面的模式应该与示例代码匹配:
Something(?:(?:<[^>]+>)|\s)*([\w\s*]+)这将匹配Something,后面跟着任何HTML (或空格)列表,并匹配下一个文本块\w (包括空格)。
您可以在PHP的preg_match()方法中使用此方法,如下所示:
if (preg_match('/Something(?:(?:<[^>]+>)|\s)*([\w\s*]+)/', $inputString, $match)) {
$matchedValue = $match[1];
// do whatever you need
}Regex解释说:
Something # has to start with 'Something'
(?: # non-matching group
(?: # non-matching group
<[^>]+> # any HTML tags, <...>
)
| \s # OR whitespace
)* # this group can match 0+ times
(
[\w\s*]+ # any non-HTML words (with/without whitespace)
)https://stackoverflow.com/questions/12732313
复制相似问题