Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何提取方括号之间的字符串_Regex_Powershell_Groovy - Fatal编程技术网

Regex 如何提取方括号之间的字符串

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(),然后只捕获第一个匹配项

我必须使用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()
,然后只捕获第一个匹配项

使用
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