2016-09-26 79 views
1

它是比較$ STRING1至特定字符串的模板在PowerShell中比較正則表達式字符串與

PS C:\ $string1 = '\\<a href="main\\.php\\?act=forum\\&hdl=read_\\&id=(\d+)\\&pid=313" class="small_name2"\\>Learn the powershell tool\\</a\\>' 
PS C:\ $string1 -match '<a href="main.php?act=forum&hdl=read_&id=83523&pid=313" class="small_name2">Learn the powershell tool</a>' 
False 

什麼是錯的模板需要在PowerShell中的特殊字符及以上也許字符串? 謝謝!

回答

2

-match運算符不可交換,所以不能切換操作數。第一個操作數必須是要與正則表達式匹配的字符串,第二個操作數必須是正則表達式。此外,正則表達式中的雙反斜槓將評估爲文字反斜槓,而不是轉義特殊字符。

更改此:

$string1 = '\\<a href="main\\.php\\?act=forum\\&hdl=read_\\&id=(\d+)\\&pid=313" class="small_name2"\\>Learn the powershell tool\\</a\\>' 
$string1 -match '<a href="main.php?act=forum&hdl=read_&id=83523&pid=313" class="small_name2">Learn the powershell tool</a>' 

到這一點:

$string1 = '\<a href="main\.php\?act=forum\&hdl=read_\&id=(\d+)\&pid=313" class="small_name2"\>Learn the powershell tool\</a\>' 
'<a href="main.php?act=forum&hdl=read_&id=83523&pid=313" class="small_name2">Learn the powershell tool</a>' -match $string1 
+1

可能要提'[正則表達式] ::逃亡()':) – briantist

+1

'[正則表達式] ::逃亡()'會有幫助,但由於表達式包含一個組((\ d +)),因此在它之前和之後的子字符串必須單獨轉義,例如像這樣:'[正則表達式] :: Escape('')' –