PowerShell:如何为重复模式创建Regex [重复]

vm0i2vca  于 2023-04-07  发布在  Shell
关注(0)|答案(1)|浏览(124)

此问题已在此处有答案

How to capture multiple regex matches, from a single line, into the $matches magic variable in Powershell?(4个答案)
昨天关门了。
我有下面的查询,我需要提取所有的列名更改,但如果我使用

$query = ",SELECT A.Title AS Identifier ,A.HOTELNAME AS PROPERTYNAME,A.REGION AS REGION,A.T0STATUS AS STATUS,"

$query -match ",.+ AS (.+),"

我只得到最后一个匹配的模式,捕获组的结果是Status,但我想要Identifier,PROPERTYNAME,REGION和STATUS。
所以基本上我的问题是如何使用正则表达式找到所有匹配而不是只有一个。
验证码:

",SELECT A.Title AS Identifier ,A.HOTELNAME AS PROPERTYNAME,A.REGION AS REGION,A.T0STATUS AS STATUS," -match ",.+ AS (.+),"
$matches

期望值:标识符PROPERTYNAME REGION STATUS。
结果:状态
另外,没有太多的文档专门用于PowerShell Regex,所以你也可以让我知道PowerShell使用哪种风格的regex。

pkwftd7m

pkwftd7m1#

对于多个匹配项,您将需要Select-String cmdlet。
标签:How to capture multiple regex matches, from a single line, into the $matches magic variable in Powershell?
关于regular expression,您可能希望使用Lookahead and Lookbehind

$query |Select-String -AllMatches '(?<=\s+AS\s+)\S+(?=\s*\,)'

$Select = $query |Select-String -AllMatches '(?<=\s+AS\s+)\S+(?=\s*\,)'
$Select.Matches.Value

Identifier
PROPERTYNAME
REGION
STATUS

相关问题