regex 获取PowerShell中inside()中的字符串的值

qzwqbdag  于 2023-08-08  发布在  Shell
关注(0)|答案(1)|浏览(119)

在一个文本文件中我有内容

build.setDisplayName(revisionNumber)

字符串
我需要读取一个文件的内容,并获得()中的值。
我试过下面的,但没有用

Select-String -Path "$PSScriptRoot/text.txt" -Pattern '(.*)?'

Select-String -Path "$PSScriptRoot/text.txt" -Pattern "(?:)".Matches.Value

注意:假设我们不知道()中的字符串是什么。我的预期输出是revisionNumber

xiozqbni

xiozqbni1#

在正则表达式中,()是特殊字符,因此如果要匹配文字括号,必须对它们进行转义

(Select-String -Path "$PSScriptRoot/text.txt" -Pattern '\(.*\)').Matches.Value

字符串
如果你想排除括号,那么使用这个

(Select-String  -Path "$PSScriptRoot/text.txt" -Pattern '\((.*)\)').Matches.Groups[1].Value

相关问题