我的文本罗马数字损坏,想使用 VBScript 修复它们。
我有以下示例字符串
some text Part I, some text Part Ii, some more text IiI, anything DXiV
我发现以下正则表达式可以很好地捕获罗马数字并且不捕获常规单词
\b(?=[MDCLXVI])(M{0,3})(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})\b
现在我正在制作一个脚本,它将用正确的(即全部大写)数字替换损坏的数字。 这是我到目前为止所拥有的:
Set re = New RegExp
re.Pattern = "\b(?=[MDCLXVI])(M{0,3})(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})\b"
re.IgnoreCase = True
re.Global = True
str = "some text Part I, some text Part Ii, some more text IiI, anything DXiV"
result = re.Replace(str,UCase("$1$2$3$4"))
我得到与输入相同的字符串。
但是如果我写
re.Replace(str,"xxx")
我会正确地将所有罗马数字替换为 xxx
。
我已经阅读了一些教程和RegExp对象文档,但仍然找不到方法。我想这不会像我想象的那么容易。
感谢您的帮助!
虽然我确信可以更简洁地完成,但这里有一个有效的版本:
Function CorrectRomanNumeralsCase(inputString)
Dim pattern, matches, match, romanNumeral, correctedRomanNumeral
pattern = "\b[IVXLCDM]+\b"
Set regex = New RegExp
regex.Pattern = pattern
regex.IgnoreCase = True
regex.Global = True
Set matches = regex.Execute(inputString)
For Each match In matches
romanNumeral = match.Value
correctedRomanNumeral = UCase(romanNumeral)
inputString = Replace(inputString, romanNumeral, correctedRomanNumeral)
Next
CorrectRomanNumeralsCase = inputString
End Function
inputString = "some text Part I, some text Part Ii, some more text IiI, anything DXiV"
correctedString = CorrectRomanNumeralsCase(inputString)
WScript.Echo correctedString
仅供参考,这是 PowerShell 版本
function Correct-RomanNumeralsCase {
param ($inputString)
$pattern = '\b([IVXLCDM]+)\b'
$matches = [regex]::Matches($inputString, $pattern)
foreach ($match in $matches) {
$romanNumeral = $match.Groups[1].Value
$correctedRomanNumeral = $romanNumeral.ToUpper()
$inputString = $inputString -replace [regex]::Escape($romanNumeral), $correctedRomanNumeral
}
return $inputString
}
$inputString = "some text Part I, some text Part Ii, some more text IiI, anything DXiV"
$correctedString = Correct-RomanNumeralsCase -inputString $inputString
Write-Host $correctedString