2012-10-04 80 views
1

我有类似以下一段HTML代码:正则表达式PCRE表达

<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"> 
      &nbsp;</td> 
     </tr> 

什么是一个很好的正则表达式字Something我以后提取第一文本节点(不是标签,但里面的文字)意思是我想提取

 needed 
    value 

没有什么更多。

我不能找出一个工作正则表达式模式在PHP中。

编辑: 我不解析整个HTML文档,但它的几行所以我要的是它使用正则表达式并没有HTML解析器做。

+1

在PHP或Perl? –

+0

“php中的模式”...谢谢:) –

+3

不要使用正则表达式解析HTML。 [见这篇文章](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)为什么。 –

回答

4

忽略用正则表达式解析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 
} 

正则表达式解释:

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) 
) 
+2

谢谢!这就是我所需要的。我觉得这个解释也很有用。 –