在“()”之间的字符串中查找单词

时间:2016-05-24 17:03:06

标签: powershell

我正在尝试使用[regex] :: matches解析字符串。我能够分开这些话,但我在开放和关闭的情况下遇到了麻烦。有人可以帮我解释我做错了什么吗?先谢谢你。

$results = "All domain controllers have migrated successfully to the Global state ('Start')."


[regex]::Matches($results , "\w+['a-z']") | foreach-object {$_.value}

2 个答案:

答案 0 :(得分:1)

稍微调整@ TheMadTechnician的正则表达式,"\('([^\)']*)'\)"将在('')之间得到单词,不带引号。我使用下面的-match运算符。您想要的结果将在$matches[1]变量中。

PS> $results = "All domain controllers have migrated successfully to the Global state ('Start')."
PS> $results -match "\('([^\)']*)'\)"
True
PS> $matches[1]
Start

答案 1 :(得分:0)

所以,这是我弄清楚如何获得这个的唯一方法。如果其他人能够提供更简单的解决方案,我全都耳朵。但就目前而言,这是我开始工作的方式

([regex]::Matches($results , '\w+[('')^]' ) | foreach-object {$_.value }).replace("'", "")

相关问题