将RegExp-string与PowerShell中的特殊字符进行比较

时间:2022-06-01 20:01:12

it is needed to compare $string1 to certain string-template in PowerShell

需要将$ 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

What is wrong in templates and maybe string above? Thank you!

模板中有什么问题,上面可能是字符串?谢谢!

1 个解决方案

#1


2  

The -match operator is not commutative, so you can't switch the operands. First operand must be the string you want to match against a regular expression, second operand must be the regular expression. Also, the double backslashes in your regular expression evaluate to literal backslashes instead of escaping special characters.

-match运算符不可交换,因此您无法切换操作数。第一个操作数必须是要与正则表达式匹配的字符串,第二个操作数必须是正则表达式。此外,正则表达式中的双反斜杠计算为文字反斜杠而不是转义特殊字符。

Change this:

$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>'

into this:

$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


2  

The -match operator is not commutative, so you can't switch the operands. First operand must be the string you want to match against a regular expression, second operand must be the regular expression. Also, the double backslashes in your regular expression evaluate to literal backslashes instead of escaping special characters.

-match运算符不可交换,因此您无法切换操作数。第一个操作数必须是要与正则表达式匹配的字符串,第二个操作数必须是正则表达式。此外,正则表达式中的双反斜杠计算为文字反斜杠而不是转义特殊字符。

Change this:

$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>'

into this:

$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