7

假设我有一个如下所示的正则表达式,但我将它从文件加载到变量 $regex 中,因此在设计时不知道它的内容是什么,但在运行时我可以发现它包含“version1”, “version2”、“version3”和“version4”命名组:

"Version (?<version1>\d),(?<version2>\d),(?<version3>\d),(?<version4>\d)"

...我有这些变量:

$version1 = "3"
$version2 = "2"
$version3 = "1"
$version4 = "0"

...我在文件中遇到以下字符串:

Version 7,7,0,0

...存储在变量 $input 中,因此 ($input -match $regex) 的计算结果为 $true。

如果我不知道它们在 $regex 中出现的顺序(我只知道 $正则表达式包括这些命名组)?

我找不到任何描述通过使用组名作为匹配索引将命名组替换为变量值的语法的参考 - 这甚至受支持吗?

编辑: 澄清 - 目标是替换任何类型的文本文件中的模板化版本字符串,其中给定文件中的版本字符串需要替换可变数量的版本字段(可能是 2、3 或所有 4 个字段)。例如,文件中的文本可能看起来像以下任何一种(但不限于这些):

#define SOME_MACRO(4, 1, 0, 0)

Version "1.2.3.4"

SomeStruct vs = { 99,99,99,99 }

用户可以指定一个文件集和一个正则表达式来匹配包含字段的行,最初的想法是各个字段将被命名组捕获。该实用程序具有应在文件中替换的各个版本字段值,但必须保留将包含替换的行的原始格式,并且仅替换请求的字段。

EDIT-2: 我认为我可以通过基于每个匹配项的位置和范围的子字符串计算得到我需要的结果,但希望 Powershell 的替换操作能够为我节省一些工作。

EDIT-3: 因此,正如 Ansgar 在下面正确而简洁地描述的那样,没有办法(仅使用原始输入字符串、您只知道命名组的正则表达式以及结果匹配项)使用“- replace" 操作(或其他正则表达式操作)来执行命名组的捕获的替换,同时保持原始字符串的其余部分不变。对于这个问题,如果有人好奇,我最终使用了下面的解决方案。YMMV,其他可能的解决方案。非常感谢 Ansgar 提供的反馈和选项。

在以下代码块中:

  • $input 是要执行替换的文本行
  • $regex 是从已验证包含至少一个受支持的命名组的文件中读取的正则表达式([string] 类型)
  • $regexToGroupName 是一个哈希表,它将一个正则表达式字符串映射到一个组名数组,该数组按照 [regex]::GetGroupNames() 返回的数组的顺序排列,与它们出现的从左到右的顺序相匹配表达方式
  • $groupNameToVersionNumber 是一个将组名映射到版本号的哈希表。

$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
4

2 回答 2

7

简单的解决方案

在您只想替换$input文本中某处的版本号的情况下,您可以简单地执行以下操作:

$input -replace '(Version\s+)\d+,\d+,\d+,\d+',"`$1$Version1,$Version2,$Version3,$Version4"

在 PowerShell 中使用命名捕获

关于您关于命名捕获的问题,可以通过使用大括号来完成。IE

'dogcatcher' -replace '(?<pet>dog|cat)','I have a pet ${pet}.  '

给出:

I have a pet dog.  I have a pet cat.  cher

多个捕获和解决方案的问题

您不能在同一个替换语句中替换多个值,因为替换字符串用于所有内容。即如果你这样做:

 'dogcatcher' -replace '(?<pet>dog|cat)|(?<singer>cher)','I have a pet ${pet}.  I like ${singer}''s songs.  '

你会得到:

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.  

...这可能不是您所希望的。

相反,您必须对每个项目进行匹配:

'dogcatcher' -replace '(?<pet>dog|cat)','I have a pet ${pet}.  ' -replace '(?<singer>cher)', 'I like ${singer}''s songs.  ' 

...要得到:

I have a pet dog.  I have a pet cat.  I like cher's songs.  

更复杂的解决方案

将其带回到您的场景中,您实际上并没有使用捕获的值;相反,您希望用新值替换它们所在的空间。为此,您只需要这个:

$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

这会给:

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).

注意:以上内容可以写成 1 行,但我已将其分解以使其更易于阅读。

这利用了正则表达式环视;一种在您捕获的字符串之前和之后检查内容的方法,而不包括匹配中的内容。也就是说,当我们选择要替换的内容时,我们可以说“匹配单词版本之后出现的数字”而不说“替换单词版本”。

有关此处的更多信息: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

给出:

#define SOME_MACRO(1, 3, 5, 7)

Version "1.3.5.7"

SomeStruct vs = { 1,3,5,7 }
于 2017-04-06T16:25:54.320 回答
4

正则表达式不能那样工作,所以你不能。不是直接的,就是这样。您可以做的(不使用更合适的正则表达式来对要保留的部分进行分组)是提取版本字符串,然后在第二步中将该子字符串替换为新版本字符串:

$oldver = $input -replace $regexp, '$1,$2,$3,$4'
$newver = $input -replace $oldver, "$Version1,$Version2,$Version3,$Version4"

编辑:

如果你甚至不知道结构,你也必须从正则表达式中提取它。

$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
于 2012-09-01T10:50:06.800 回答