sed - Linux中的正则表达式方括号检测

时间:2016-07-31 13:41:39

标签: bash sed

我正在使用Ubuntu 14.04,我有以下声明:

192.168.2.4 [text to capture] Test: This is a test statement.

我正在尝试使用以下正则表达式捕获“要捕获的文本”:

echo "192.168.2.4 [text to capture] Test: This is a test statement" | sed -r "s/^[^\[\]]*\[(.*)\].*$/\1/"

正则表达式背后的想法是遍历所有与开始和结束方括号不匹配的字符。遇到开头方括号后,捕获文本直到遇到右括号,然后忽略所有后续字符。

当我在regex tester中使用上面的正则表达式时,我可以看到正在捕获“要捕获的文本”。

但是,执行上面的regex命令会返回完整语句aka:

echo "192.168.2.4 [text to capture] Test: This is a test statement" | sed -r "s/^[^\[\]]*\[(.*)\].*$/\1/"

有人能发现我在这里错过的东西吗?我相信我已经正确地转义了字符括号,因为它与正则表达式测试器一起正常工作。

由于 约翰

5 个答案:

答案 0 :(得分:4)

您可以使用此sed:

echo "192.168.2.4 [text to capture] Test: This is a test statement" |
sed -r 's/^[^[]*\[([^]]*)\].*$/\1/'

text to capture

但是为了简单起见,我建议使用awk来避免复杂的正则表达式:

echo "192.168.2.4 [text to capture] Test: This is a test statement" |
awk -F '[][]' '{print $2}'

text to capture

这是gnu grep替代方案(虽然建议使用awk):

echo "192.168.2.4 [text to capture] Test: This is a test statement" |
grep -oP '[^][]+(?=\])'

text to capture

答案 1 :(得分:2)

您实际上只需要排除第一个语句中的起始[

echo "192.168.2.4 [text to capture] Test: This is a test statement" | sed -r "s/^[^[]*\[(.*)\].*$/\1/"

如果你真的想在[中同时使用][^ ],只需使用[^][],就不需要转义。

答案 2 :(得分:2)

$ echo "192.168.2.4 [text to capture] Test: This is a test statement" |
sed -E 's/.*\[([^]]*)\].*/\1/'
text to capture

如果您正在使用gnu-sed,请注意使用未记录的-E选项,该选项启用扩展正则表达式

答案 3 :(得分:1)

$ echo "192.168.2.4 [text to capture] Test: This is a test statement" |
  sed -E 's/.*\[([^]]+).*/\1/'
text to capture

$ echo "192.168.2.4 [text to capture] Test: This is a test statement" |
  sed -E 's/.*\[(.*)\].*/\1/'
text to capture

答案 4 :(得分:0)

这是在Linux中使用“cut”命令在括号内提取文本的另一种方法。第一个“剪切”提取在第一个正方形(开口)括号后出现的文本,而第二个剪切从第一个剪切语句的输出中提取出现在结束方括号之前的文本。

echo "192.168.2.4 [text to capture] Test: This is a test statement" | cut -d"[" -f2 | cut -d"]" -f1
text to capture

由于

约翰