Regex 如何提取方括号之间的字符串
我必须使用Powershell或Groovy脚本从方括号中提取字符串 PowerShell:Regex 如何提取方括号之间的字符串,regex,powershell,groovy,Regex,Powershell,Groovy,我必须使用Powershell或Groovy脚本从方括号中提取字符串 PowerShell: $string = "[test][OB-110] this is some text" $found = $string -match '(?<=\[)[^]]+(?=\])' echo $matches 我想让它退回这个: test OB-110 test OB-110 我需要提取括号内的所有文本。-match将在后台内部调用Regex.match(),然后只捕获第一个匹配项
$string = "[test][OB-110] this is some text"
$found = $string -match '(?<=\[)[^]]+(?=\])'
echo $matches
我想让它退回这个:
test
OB-110
test
OB-110
我需要提取括号内的所有文本。
-match
将在后台内部调用Regex.match()
,然后只捕获第一个匹配项
使用Select String
和-AllMatches
开关:
($string |Select-String '(?<=\[)[^]]+(?=\])' -AllMatches).Matches.Value
对于Groovy:
def str = "[test][OB-110] this is some text"
str.findAll(/(?<=\[)[^]]+(?=\])/).each {
println it
}
def str = "[test][OB-110] this is some text"
str.findAll(/(?<=\[)[^]]+(?=\])/).each {
println it
}
test
OB-110