考虑以下通用正则表达式和逻辑的 powershell 示例。这不使用任何正则表达式外观,并且将匹配任何blah blah
行上的 ABCD。
您应该能够将此概念重写到您的 VBA 逻辑中。
例子
$Matches = @()
$String = 'SAMPLE
ITEM_ID sample_id_0000028
blah blah
ABCD <--- do NOT remove
blah blah blah
blah blah
blah
SAMPLE_END
SAMPLE
ITEM_ID sample_id_0000033
other text
more text
ABCD <--- Remove this
more text
SAMPLE_END
SAMPLE
ITEM_ID sample_id_00041
ABCD <--- do NOT remove
blah blah blah
blah
SAMPLE_END
SAMPLE
ITEM_ID sample_id_0000028
blah blah
ABCD <--- do NOT remove
blah blah blah
blah blah
blah
SAMPLE_END
SAMPLE
ITEM_ID sample_id_0000033
other text
more text
ABCD <--- Remove this
more text
SAMPLE_END
SAMPLE
ITEM_ID sample_id_00041
ABCD <--- do NOT remove
blah blah blah
blah
SAMPLE_END'
$NewString = $String
([regex]'(sample_id_0000033((.|\n|\r)*?)SAMPLE_END)').matches($String) | foreach {
write-host --------------------------------------------
Write-Host "found at $($_.Groups[1].Index) = '$($_.Groups[1].Value)'"
Write-Host "found at $($_.Groups[2].Index) = '$($_.Groups[2].Value)'"
$ThisRecord = $_.Groups[1].Value
$InnerText = $_.Groups[2].Value
$NewInnerText = $InnerText -replace "ABCD", "I like kittens"
$NewRecord = $ThisRecord -replace $InnerText, $NewInnerText
write-host
Write-Host NewRecord:
Write-Host $NewRecord
$NewString = $NewString -replace $ThisRecord, $NewRecord
} # next match
产量
请注意,在此示例中,我将<--- Remove this
值保留在字符串上,以便更容易识别更改的位置
--------------------------------------------
found at 136 = 'sample_id_0000033
other text
more text
ABCD <--- Remove this
more text
SAMPLE_END'
found at 153 = '
other text
more text
ABCD <--- Remove this
more text
'
NewRecord:
sample_id_0000033
other text
more text
I like kittens <--- Remove this
more text
SAMPLE_END
--------------------------------------------
found at 452 = 'sample_id_0000033
other text
more text
ABCD <--- Remove this
more text
SAMPLE_END'
found at 469 = '
other text
more text
ABCD <--- Remove this
more text
'
NewRecord:
sample_id_0000033
other text
more text
I like kittens <--- Remove this
more text
SAMPLE_END
--------------------------------------------
New String
SAMPLE
ITEM_ID sample_id_0000028
blah blah
ABCD <--- do NOT remove
blah blah blah
blah blah
blah
SAMPLE_END
SAMPLE
ITEM_ID sample_id_0000033
other text
more text
I like kittens <--- Remove this
more text
SAMPLE_END
SAMPLE
ITEM_ID sample_id_00041
ABCD <--- do NOT remove
blah blah blah
blah
SAMPLE_END
SAMPLE
ITEM_ID sample_id_0000028
blah blah
ABCD <--- do NOT remove
blah blah blah
blah blah
blah
SAMPLE_END
SAMPLE
ITEM_ID sample_id_0000033
other text
more text
I like kittens <--- Remove this
more text
SAMPLE_END
SAMPLE
ITEM_ID sample_id_00041
ABCD <--- do NOT remove
blah blah blah
blah
SAMPLE_END
概括
- 使用此正则表达式
(sample_id_0000033((.|\n|\r)*?)SAMPLE_END)
查找以 sample_id_0000033 开头并以下一个 SAMPLE_END 结尾的所有文本块。当然,如果您对记录结束使用不同的分隔符,您也需要在此处包含该分隔符。
- 在幕后,Powershell 隐藏了它如何
$Matches
使用所有找到的子字符串填充数组。然后将它们传递到相当于 $Matches的foreach
循环中(在这种情况下)。$_
- 在
foreach
块内,我们处理每个找到的匹配实例:
ABCD
用所需的字符串替换已知文本I like kittens
并将结果更改存储到$NewInnerText
. 我在这里创建了一个新变量,因为$InnerText
它不包括打开和关闭字符串,这取决于您的实际值ABCD
可能会意外更改结束标签中的文本。
- 是根据用inside
$NewRecord
替换的结果创建的$InnerText
$NewInnerText
$ThisRecord
- 然后
$NewString
我们发出替换$ThisRecord
$NewRecord