【问题标题】:powershell regex bugpowershell 正则表达式错误
【发布时间】:2021-06-04 21:19:32
【问题描述】:

我有以下代码:

#look up the app function name for this resource group
switch -Regex (az resource list -g $RESOURCE_NAME -o table) {
    '^(\w+[.-]*\w+).*\bserverFarms\b' {
      $Matches[1]; break
    }
  }
$FUNCTION_APP = $Matches[1]
echo $Matches.Count
echo $Matches[0]
echo $Matches[1]

az resource list 命令返回以下输出:

> az resource list -g $AZ_RESOURCE_GROUP_NAME -o table
Name                   ResourceGroup    Location    Type                               
----------------------  ---------------  ----------  --------------------------------- 
jf-aa-ext-app-12345      jfFuncs         eastus      microsoft.insights/components
omstorage                jfFuncs         eastus      Microsoft.Storage/storageAccounts
jf-aa-ext-app-12345      jfFuncs         eastus      Microsoft.Web/serverFarms
jf-aa-ext-app-12345     jfFuncs         eastus      Microsoft.Web/sites

尝试从包含“serverFarms”的行中提取“jf-aa-ext-app-12345”。 但它只返回“jf-aa”作为匹配项。

【问题讨论】:

  • ^(\S*).*\bserverFarms\b 是否提供您需要的结果?
  • the question you asked and got that code 中,多人推荐使用 Azure 模块来获取此信息,而不是解析 cli 输出。为什么不用模块?
  • @TheMadTechnician 因为这段代码......最终将需要在 docker 容器中运行,我可能无法选择安装 azure 模块......我知道它会有 cli......
  • 我找到了我需要的正则表达式。 '^([\w+.-]*).*\bserverFarms\b'
  • az 有一个 --output json 选项,使用 ConvertFrom-Json 更容易解析 - 不需要正则表达式。从那里,您可以通过where-object 走向胜利...

标签: regex powershell


【解决方案1】:

编辑

在顶部添加此编辑,因为它显然是最佳答案,我不知道 Azure CLI 可以输出 json,谢谢mclayton

# Some alternatives using "-o json"

az resource list -g $RESOURCE_NAME -o json | ConvertFrom-Json |
Where-Object {$_.Type -match 'serverFarms'} | Select -Expand Name

# OR

$resGroup = az resource list -g $RESOURCE_NAME -o json | ConvertFrom-Json
$resGroup.where({$_.Type -match 'serverFarms'}).Name

当然,我为您的问题提出了这个解决方案,就像昨天的 cmets Ash 推荐使用绝对推荐的 Az Module 一样。解析此输出可在未来情况下重复使用的另一种方法是将其转换为可以轻松操作的对象(这是TheMadTechnician 提出的建议,在我看来这比使用特定的regex 来获得要好得多你想要的结果)。

注意:我正在拆分 '\r\n',因为我使用的是从您的问题中粘贴的字符串副本。在您的情况下,只需将az resource... 的结果添加到Select-String 应该可以正常工作。

$output = @'
Name                   ResourceGroup    Location    Type                               
----------------------  ---------------  ----------  --------------------------------- 
jf-aa-ext-app-12345      jfFuncs         eastus      microsoft.insights/components
omstorage                jfFuncs         eastus      Microsoft.Storage/storageAccounts
jf-aa-ext-app-12345      jfFuncs         eastus      Microsoft.Web/serverFarms
jf-aa-ext-app-12345      jfFuncs         eastus      Microsoft.Web/sites
'@ -split '\r\n' | Select-String -Pattern '^[^-]'

$parsedOutput = $output -replace '\s{2,}',',' | ConvertFrom-Csv

输出:

PS /> $parsedOutput

Name                ResourceGroup Location Type                             
----                ------------- -------- ----                             
jf-aa-ext-app-12345 jfFuncs       eastus   microsoft.insights/components    
omstorage           jfFuncs       eastus   Microsoft.Storage/storageAccounts
jf-aa-ext-app-12345 jfFuncs       eastus   Microsoft.Web/serverFarms        
jf-aa-ext-app-12345 jfFuncs       eastus   Microsoft.Web/sites              

PS /> $parsedOutput | Get-Member -MemberType NoteProperty

   TypeName: System.Management.Automation.PSCustomObject

Name          MemberType   Definition                               
----          ----------   ----------                               
Location      NoteProperty string Location=eastus                   
Name          NoteProperty string Name=jf-aa-ext-app-12345          
ResourceGroup NoteProperty string ResourceGroup=jfFuncs             
Type          NoteProperty string Type=microsoft.insights/components

你想要的结果:

PS /> $parsedOutput.where({$_.Type -match 'serverFarms'}).Name
jf-aa-ext-app-12345

【讨论】:

  • 我很感激。但最终,我必须在 dockerized 环境中完成这项工作,在该环境中我可能无法选择仅安装 azure 模块。这就是为什么我一直在继续使用我知道将可用的工具。不过谢谢
  • @dot yup,这就是为什么我向您展示如何使用已有的内容解析输出。
  • 我最终只是使用 mclaytons 建议以 JSON 格式输出。如果您可以在此处将其添加到您的答案中,我会接受!非常感谢您的帮助!
  • @dot 完成,我将它添加到顶部,因为它显然是最好的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-29
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
相关资源
最近更新 更多