我试图从网站上的脚本标签中获取一定数量的字符串(每次重新加载时数字的长度不同)。但是,我正在努力弄清楚如何做到这一点,因为我被PowerShell v2困住,无法升级它。
我已经设法通过在IE中加载网站获取元素并获取按标签名称“script”的元素来获取完整脚本,并且我试图尝试使用一些正则表达式来查找字符串但是无法弄明白。
我也尝试从脚本的正面和背面剥离字符,那时我才意识到每次数字的长度都会改变。
部分脚本是:
var value = document.wizform.selActivities.options[document.wizform.selActivities.selectedIndex].value;
if (value == "Terminate") {
if (confirm("Are you sure you want to terminate the selected business process(es)?")) {
document.wizform.action = "./Page?next=page.actionrpt&action=terminate&pos=0&1006999619";
javascript:document.wizform.submit();
}
} else if (value == "TerminateAndRestart") {
if (confirm("Are you sure you want to terminate and restart the selected business process(es)?")) {
document.wizform.action = "./Page?next=page.actionrpt&action=terminateandrestart&pos=0&237893352";
javascript:document.wizform.submit();
}
}
我要捕捉的部分是这里的数字
document.wizform.action = "./Page?next=page.actionrpt&action=terminateandrestart&pos=0&237893352";
到目前为止我的PowerShell代码是
$checkbox = $ie.Document.getElementsByTagName("script") | Where-Object {
$_.outerHTML -like "*./Page?next=page.actionrpt&action=terminate*"
} # | select -Expand outerHTML
$content = $checkbox
$matches = [regex]::Matches($content, '".\action=terminate\.([^"]+)')
$matches | ForEach-Object {
$_.Groups[1].Value
}
我想要的是PowerShell只有数字作为变量,所以在上面的例子中我希望能够有0&237893352
或只是237893352
(因为注释没有改变,所以我可以添加0&
后如果我需要)。
使用正面的lookbehind断言来匹配您感兴趣的特定操作:
$re = '(?<=action=terminateandrestart&pos=)0&\d+'
$content |
Select-String -Pattern $re |
Select-Object -Expand Matches |
Select-Object -Expand Value
(?<=...)
是一个名为“positive lookbehind assertion”的正则表达式构造,它允许匹配某个特定字符串前面的东西(在你的情况下为“action = terminateandrestart&pos =”),而不会使该字符串成为返回匹配的一部分。通过这种方式,您可以查找字符串“action = terminateandrestart&pos =”后跟“0&”和一个或多个数字(\d+
)并仅返回“0&”和数字。