如何在括号之间提取精确的字符串?
我尝试的是:
echo "test [test1] test" | grep -Po "(?=\[).*?(?=\])"
Run Code Online (Sandbox Code Playgroud)
但输出是:
[test1
Run Code Online (Sandbox Code Playgroud)
它应该是:
test1
Run Code Online (Sandbox Code Playgroud)
更好用grep.
使用lookbehind:
echo "test [test1] test" | grep -Po "(?<=\[).*?(?=\])"
Run Code Online (Sandbox Code Playgroud)