2012-04-16 105 views
1

我希望它可以,我问这个问题,我搜索了周围的计算器,发现了相似的问题,但没有解决方案为我工作。正则表达式:匹配H1标签后的冒号?

我有这样的HTML: <h1>Beatles: A Hard Days Night</h1>现在我想要一个正则表达式来匹配冒号后的所有内容。所以在这种情况下A Hard Days Night

这是我的尝试:

$pattern = "/<h1>\:(.*)<\/h1>/"; 

但这只是输出一个空数组。

回答

4

下面的正则表达式应该匹配:

<h1>[^:]+:\s+([^<]+) 

PowerShell的测试:

PS> '<h1>Beatles: A Hard Days Night</h1>' -match '<h1>[^:]+:\s+([^<]+)'; $Matches 
True 

Name       Value 
----       ----- 
1        A Hard Days Night 
0        <h1>Beatles: A Hard Days Night 

一点解释:

<h1> # match literal <h1> 
[^:]+ # match everything *before* the colon (which in this case 
     # shouldn't include a colon itself; if it does, then use .*) 
:  # Literal colon 
\s+  # Arbitrary amount of whitespace 
([^<]+) # Put everything up to the next < into a capturing group.