regex - 在 powershell 中按正则表达式内的命名捕获组排序

标签 regex powershell sorting capture-group

我有一个正则表达式定义如下:

$regexpo = "^Amma\s(?'version'(\d+\.){3}\d)\.zip$"

上面的表达式附加了一个组标签 - (\d+.){3}\d)

我使用它的方式如下:

$Library =  $wholeContent | 
  Where-Object { $_.Name -match $regexpo } | 
    Sort-Object Name -Descending

但我想按定义的正则表达式中的标记组对其进行排序,它代表版本号。

最佳答案

Sort-Object 与计算属性(针对每个输入对象进行评估的脚本 block ({ ... }),反射(reflect)在 $_ 中)一起使用,如下所示:

# Sample input
$wholeContent = 
  [pscustomobject] @{ Name = 'Amma 10.0.0.1.zip'; Type = '...' },
  [pscustomobject] @{ Name = 'Not a match' ; Type = '...' },
  [pscustomobject] @{ Name = 'Amma 1.0.0.2.zip' ; Type = '...' },
  [pscustomobject] @{ Name = 'Amma 2.1.2.3.zip' ; Type = '...' }

# Define the regex that matches the full name
# and captures the embedded version number in named capture group 'version'.
# Better to use '...' (single quotes) to define regexes, to prevent
# confusion with string expansion inside "..."
# Note the alternate syntax `<version>` instead of `'version'`.
$regex = '^Amma\s(?<version>(\d+\.){3}\d+)\.zip$'

# Filter by the line of interest, then sort by the extracted version number.
# Automatic variable $Matches is a hashtable that contains the results of
# the regex match, with entry 'version' containing the capture group's value. 
# Casting to [version] ensures that version-appropriate sorting is used.
$wholeContent |
  Where-Object { $_.Name -match $regex } | 
    Sort-Object { [version] ($_.Name -replace $regex, '${version}') }

请注意,此处需要匹配两次[1]:一次用于过滤感兴趣的行,再次通过 -replace operator 提取嵌入的版本文本。

注意:此处可以将 -replace 与原始正则表达式一起使用,因为手头的正则表达式设计为匹配整个输入字符串,这允许将整个字符串替换为指定捕获组的值 (${version}),以仅生成后者;更详细的替代方法是使用另一个 -match 操作通过 $Matches 获取捕获组值:
$null = $_.Name -match $regex; $Matches['version']

上面的结果如下,显示仅提取了感兴趣的行,并按版本号正确排序:

Name              Type
----              ----
Amma 1.0.0.2.zip  ...
Amma 2.1.2.3.zip  ...
Amma 10.0.0.1.zip ...

[1] 虽然由 -match 操作填充的 automatic $Matches variable 原则上可以在后续管道段的脚本 block 中使用,从而允许访问匹配操作的结果,这里不能使用它,因为 Sort-Object 必然是一个聚合 cmdlet;也就是说,它必须首先收集所有输入才能执行排序,此时在计算属性中使用$Matches仅包含最后输入对象的匹配项。

关于regex - 在 powershell 中按正则表达式内的命名捕获组排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59804926/

相关文章:

regex - 如何在 Emacs Lisp 中用字符串中的反斜杠替换正斜杠?

windows - 在 Windows 10 分配的访问权限中运行 Chrome 信息亭模式

Powershell Bool 返回数组

javascript - 使用 JavaScript 对数字进行排序

javascript - 如何在没有手动索引的情况下对 Javascript 中的字母等级进行排序

javascript - JavaScript作业题中的自定义排序

java - 从句子中提取单词的方法

regex - 在终端上使用正则表达式分组提取字符串

java - 正则表达式维基表

testing - 在 Windows Powershell 中选择用于 Web UI 测试自动化的下拉列表选项