我需要在某个字符串之后从网站脚本中获取数字
I need to get numbers out of a website script after a certain string
我试图从网站上的脚本标签中获取特定的数字字符串(每次重新加载时数字的长度都不同)。但是,由于我受困于 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&
。
使用积极的回顾断言来匹配您感兴趣的特定操作:
$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=")的内容,而无需将该字符串作为return匹配。通过这种方式,您可以查找字符串 "action=terminateandrestart&pos=" 后跟“0&”和一位或多位数字 (\d+
),而 return 仅查找“0&”和数字。
我试图从网站上的脚本标签中获取特定的数字字符串(每次重新加载时数字的长度都不同)。但是,由于我受困于 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&
。
使用积极的回顾断言来匹配您感兴趣的特定操作:
$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=")的内容,而无需将该字符串作为return匹配。通过这种方式,您可以查找字符串 "action=terminateandrestart&pos=" 后跟“0&”和一位或多位数字 (\d+
),而 return 仅查找“0&”和数字。