Hoo*_*oob 5 regex powershell named regex-group
假设我有一个如下所示的正则表达式,但我将它从一个文件加载到一个变量$ regex中,所以在设计时不知道它的内容是什么,但在运行时我发现它包含"version1", "version2","version3"和"version4"命名组:
"Version (?<version1>\d),(?<version2>\d),(?<version3>\d),(?<version4>\d)"
Run Code Online (Sandbox Code Playgroud)
......我有这些变量:
$version1 = "3"
$version2 = "2"
$version3 = "1"
$version4 = "0"
Run Code Online (Sandbox Code Playgroud)
...我在文件中遇到以下字符串:
Version 7,7,0,0
Run Code Online (Sandbox Code Playgroud)
...存储在变量$ input中,因此($ input -match $ regex)的计算结果为$ true.
如果我不知道它们出现在$ regex中的顺序,我怎么能用字符串$ input中的$ regex替换$ version1,$ version2,$ version3,$ version4的值中的命名组(我只知道$正则表达式包括这些命名组)?
我找不到任何描述使用组名作为匹配索引用变量值替换命名组的语法的引用 - 这是否支持?
编辑: 澄清 - 目标是替换任何类型的文本文件中的模板版本字符串,其中给定文件中的版本字符串需要替换可变数量的版本字段(可能是2,3或所有4个字段).例如,文件中的文本可能看起来像这些中的任何一个(但不限于这些):
#define SOME_MACRO(4, 1, 0, 0)
Version "1.2.3.4"
SomeStruct vs = { 99,99,99,99 }
Run Code Online (Sandbox Code Playgroud)
用户可以指定文件集和正则表达式以匹配包含字段的行,最初的想法是命名组将捕获各个字段.该实用程序具有应在文件中替换的各个版本字段值,但必须保留将包含替换的行的原始格式,并仅替换所请求的字段.
EDIT-2: 我想我可以根据每个匹配的位置和范围得到我需要的子串计算结果,但希望Powershell的替换操作能够为我节省一些工作.
EDIT-3: 所以,正如Ansgar在下面正确而简洁地描述的那样,没有办法(只使用原始输入字符串,一个正则表达式,你只知道命名组,以及产生的匹配)来使用" -替换"操作(或其他正则表达式操作)以执行命名组的捕获的替换,同时保留原始字符串的其余部分.对于这个问题,如果有人好奇,我最终使用下面的解决方案.YMMV,其他解决方案可行.非常感谢Ansgar提供的反馈和选择.
在以下代码块中:
$ regex中命名组的约束只是(我认为)命名组中的表达式不能嵌套,并且在输入字符串中最多应匹配一次.
# This will give us the index and extent of each substring
# that we will be replacing (the parts that we will not keep)
$matchResults = ([regex]$regex).match($input)
# This will hold substrings from $input that were not captured
# by any of the supported named groups, as well as the replacement
# version strings, properly ordered, but will omit substrings captured
# by the named groups
$lineParts = @()
$startingIndex = 0
foreach ($groupName in $regexToGroupName.$regex)
{
# Excise the substring leading up to the match for this group...
$lineParts = $lineParts + $input.Substring($startingIndex, $matchResults.groups[$groupName].Index - $startingIndex)
# Instead of the matched substring, we'll use the substitution
$lineParts = $lineParts + $groupNameToVersionNumber.$groupName
# Set the starting index of the next substring that we will keep...
$startingIndex = $matchResults.groups[$groupName].Index + $matchResults.groups[$groupName].Length
}
# Keep the end of the original string (if there's anything left)
$lineParts = $lineParts + $input.Substring($startingIndex, $input.Length - $startingIndex)
$newLine = ""
foreach ($part in $lineParts)
{
$newLine = $newLine + $part
}
$input= $newLine
Run Code Online (Sandbox Code Playgroud)
简单的解决方案
在您只想替换$input文本中某处的版本号的情况下,您可以简单地执行以下操作:
$input -replace '(Version\s+)\d+,\d+,\d+,\d+',"`$1$Version1,$Version2,$Version3,$Version4"
Run Code Online (Sandbox Code Playgroud)
在 PowerShell 中使用命名捕获
关于您关于命名捕获的问题,可以通过使用大括号来完成。IE
'dogcatcher' -replace '(?<pet>dog|cat)','I have a pet ${pet}. '
Run Code Online (Sandbox Code Playgroud)
给出:
I have a pet dog. I have a pet cat. cher
Run Code Online (Sandbox Code Playgroud)
多次捕获和解决方案的问题
您不能在同一个替换语句中替换多个值,因为替换字符串用于所有内容。即如果你这样做:
'dogcatcher' -replace '(?<pet>dog|cat)|(?<singer>cher)','I have a pet ${pet}. I like ${singer}''s songs. '
Run Code Online (Sandbox Code Playgroud)
你会得到:
I have a pet dog. I like 's songs. I have a pet cat. I like 's songs. I have a pet . I like cher's songs.
Run Code Online (Sandbox Code Playgroud)
......这可能不是你所希望的。
相反,您必须对每个项目进行匹配:
'dogcatcher' -replace '(?<pet>dog|cat)','I have a pet ${pet}. ' -replace '(?<singer>cher)', 'I like ${singer}''s songs. '
Run Code Online (Sandbox Code Playgroud)
...要得到:
I have a pet dog. I have a pet cat. I like cher's songs.
Run Code Online (Sandbox Code Playgroud)
更复杂的解决方案
回到您的场景中,您实际上并没有使用捕获的值;相反,您希望用新值替换它们所在的空间。为此,您只需要这样:
$input = 'I''m running Programmer''s Notepad version 2.4.2.1440, and am a big fan. I also have Chrome v 56.0.2924.87 (64-bit).'
$version1 = 1
$version2 = 3
$version3 = 5
$version4 = 7
$v1Pattern = '(?<=\bv(?:ersion)?\s+)\d+(?=\.\d+\.\d+\.\d+)'
$v2Pattern = '(?<=\bv(?:ersion)?\s+\d+\.)\d+(?=\.\d+\.\d+)'
$v3Pattern = '(?<=\bv(?:ersion)?\s+\d+\.\d+\.)\d+(?=\.\d+)'
$v4Pattern = '(?<=\bv(?:ersion)?\s+\d+\.\d+\.\d+\.)\d+'
$input -replace $v1Pattern, $version1 -replace $v2Pattern, $version2 -replace $v3Pattern,$version3 -replace $v4Pattern,$version4
Run Code Online (Sandbox Code Playgroud)
这会给:
I'm running Programmer's Notepad version 1.3.5.7, and am a big fan. I also have Chrome v 1.3.5.7 (64-bit).
Run Code Online (Sandbox Code Playgroud)
注意:上述内容可以写成 1 行,但我已将其分解以使其更易于阅读。
这利用了正则表达式的环视;一种在您捕获的字符串之前和之后检查内容的方法,但不包括匹配项中的内容。也就是说,当我们选择要替换的内容时,我们可以说“匹配出现在单词 version 之后的数字”,而不必说“替换单词 version”。
有关此处的更多信息:http : //www.regular-expressions.info/lookaround.html
你的榜样
使上述内容适用于您的示例(即版本可以用逗号或点分隔,并且除了 4 组数字之外,它们的格式没有一致性:
$input = @'
#define SOME_MACRO(4, 1, 0, 0)
Version "1.2.3.4"
SomeStruct vs = { 99,99,99,99 }
'@
$version1 = 1
$version2 = 3
$version3 = 5
$version4 = 7
$v1Pattern = '(?<=\b)\d+(?=\s*[\.,]\s*\d+\s*[\.,]\s*\d+\s*[\.,]\s*\d+\b)'
$v2Pattern = '(?<=\b\d+\s*[\.,]\s*)\d+(?=\s*[\.,]\s*\d+\s*[\.,]\s*\d+\b)'
$v3Pattern = '(?<=\b\d+\s*[\.,]\s*\d+\s*[\.,]\s*)\d+(?=\s*[\.,]\s*\d+\b)'
$v4Pattern = '(?<=\b\d+\s*[\.,]\s*\d+\s*[\.,]\s*\d+\s*[\.,]\s*)\d+\b'
$input -replace $v1Pattern, $version1 -replace $v2Pattern, $version2 -replace $v3Pattern,$version3 -replace $v4Pattern,$version4
Run Code Online (Sandbox Code Playgroud)
给出:
#define SOME_MACRO(1, 3, 5, 7)
Version "1.3.5.7"
SomeStruct vs = { 1,3,5,7 }
Run Code Online (Sandbox Code Playgroud)
正则表达式不是这样工作的,所以你不能。不是直接的,就是这样。您可以做的(除了使用更合适的正则表达式对要保留的部分进行分组之外)是提取版本字符串,然后在第二步中将该子字符串替换为新版本字符串:
$oldver = $input -replace $regexp, '$1,$2,$3,$4'
$newver = $input -replace $oldver, "$Version1,$Version2,$Version3,$Version4"
Run Code Online (Sandbox Code Playgroud)
编辑:
如果您甚至不知道结构,则还必须从正则表达式中提取它。
$version = @($version1, $version2, $version3, $version4)
$input -match $regexp
$oldver = $regexp
$newver = $regexp
for ($i = 1; $i -le 4; $i++) {
$oldver = $oldver -replace "\(\?<version$i>\\d\)", $matches["version$i"]
$newver = $newver -replace "\(\?<version$i>\\d\)", $version[$i-1]
}
$input -replace $oldver, $newver
Run Code Online (Sandbox Code Playgroud)