复杂的查找/替换为PowerShell
我想在以下示例中添加html链接:复杂的查找/替换为PowerShell,powershell,Powershell,我想在以下示例中添加html链接: 诸如此类,诸如此类 废话废话废话 废话废话BZ 012345废话废话 诸如此类的复杂事物诸如此类的 因此,这个问题看起来像: 废话废话废话废话 废话废话废话废话 废话废话废话废话 废话复杂的废话,废话 所以我想这是一个查找匹配项的正则表达式工作,还有一些PowerShell魔术来添加html链接。我认为正则表达式是((BZ#)|(BZ))[0-9]+来匹配除复杂示例之外的所有内容,但我对正则表达式的了解还不足以确定,甚至不知道如何在PowerShell中测试它
所以我想这是一个查找匹配项的正则表达式工作,还有一些PowerShell魔术来添加html链接。我认为正则表达式是
((BZ#)|(BZ))[0-9]+
来匹配除复杂示例之外的所有内容,但我对正则表达式的了解还不足以确定,甚至不知道如何在PowerShell中测试它。我认为这是可行的,这取决于输入的变量
$inputstring = @'
Blah Blah Blah BZ#01234 Blah Blah Blah
Blah Blah Blah BZ#0124 Blah Blah Blah
Blah Blah Blah BZ 012345 Blah Blah Blah
Blah Complex Blah BZ#123 , 345, 567,2341 Blah
'@
# Split the input into an array of strings
$lines = $inputstring -split '[\r\n]'
# Setup patterns to match against
$pattern1 = "(.*)\sBZ#(\d+) , (\d+), (\d+),(\d+) (.*)"
$pattern2 = "(.*)\sBZ#(\d+)\s(.*)"
$pattern3 = "(.*)\sBZ (\d+)\s(.*)"
# Loop through each line
for($i = 0; $i -lt $lines.count; $i++)
{
# Start off assuming the current line produces no output
$output = ""
# Look for volumes with drive letters
if($lines[$i] -match $pattern1)
{
$output = $matches[1] + " <a href=`"http://bz.com/show_bug.cgi?id=" + $matches[2] + "`">BZ#" + $matches[2] + "</a> , " +
"<a href=`"http://bz.com/show_bug.cgi?id=" + $matches[3] + "`">" + $matches[3] + "</a>, " +
"<a href=`"http://bz.com/show_bug.cgi?id=" + $matches[4] + "`">" + $matches[4] + "</a>," +
"<a href=`"http://bz.com/show_bug.cgi?id=" + $matches[5] + "`">" + $matches[5] + "</a> " +
$matches[6]
}
elseif($lines[$i] -match $pattern2)
{
$output = $matches[1] + " <a href=`"http://bz.com/show_bug.cgi?id=" + $matches[2] + "`">BZ#" + $matches[2] + "</a> " + $matches[3]
}
elseif($lines[$i] -match $pattern3)
{
$output = $matches[1] + " <a href=`"http://bz.com/show_bug.cgi?id=" + $matches[2] + "`">BZ " + $matches[2] + "</a> " + $matches[3]
}
# Write out any output that was produced
if($output -ne "")
{
$output
}
}
$inputstring=@'
诸如此类,诸如此类
废话废话废话
废话废话BZ 012345废话废话
诸如此类的复杂事物诸如此类的
'@
#将输入拆分为字符串数组
$lines=$inputstring-拆分'[\r\n]'
#设置要匹配的模式
$pattern1=“(.*)\sBZ~(\d+),(\d+),(\d+),(\d+),(\d+)(.*
$pattern2=“(.*)\sBZ#(\d+)\s(.*)”
$pattern3=“(.*)\sBZ(\d+)\s(.*)”
#每行循环一次
对于($i=0;$i-lt$lines.count;$i++)
{
#假设当前线路不产生输出,则启动
$output=“”
#查找包含驱动器号的卷
if($line[$i]-匹配$pattern1)
{
$output=$matches[1]+“,”+
", " +
"," +
" " +
$matches[6]
}
elseif($line[$i]-匹配$pattern2)
{
$output=$matches[1]+“”+$matches[3]
}
elseif($line[$i]-匹配$pattern3)
{
$output=$matches[1]+“”+$matches[3]
}
#写出所产生的任何输出
如果($output-ne“”)
{
$output
}
}
输出:
Blah Blah Blah <a href="http://bz.com/show_bug.cgi?id=01234">BZ#01234</a> Blah Blah Blah
Blah Blah Blah <a href="http://bz.com/show_bug.cgi?id=0124">BZ#0124</a> Blah Blah Blah
Blah Blah Blah <a href="http://bz.com/show_bug.cgi?id=012345">BZ 012345</a> Blah Blah Blah
Blah Complex Blah <a href="http://bz.com/show_bug.cgi?id=123">BZ#123</a> , <a href="http://bz.com/show_bug.cgi?id=345">345</a>, <a href="http://bz.com/show_bug.cgi?id=567">567</a>,<a href="http://bz.com/show_bug.cgi?id=2341">2341</a> Blah
废话废话废话
废话废话废话废话
废话废话废话废话
废话复杂的废话,废话