如何删除方括号里面和任何文字? [英] How to remove square brackets and any text inside?

查看:217
本文介绍了如何删除方括号里面和任何文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含在方括号内一些文本,例如文档:

I have a document containing some text inside square brackets, e.g.:

The fish [ate] the bird.
[This is some] text.
Here is a number [1001] and another [1201].

我要删除所有包含的平方布拉克和brakets,例如中的信息:

I need to delete all of the information contained inside the square brack and the brakets, e.g.:

The fish  the bird.
 text.
Here is a number  and another .


  • 我试过的sed -r的/ \\ [[+] \\] // G'file.txt的,但这并没有工作。

    • I tried sed -r 's/\[[+]\]//g' file.txt, but this did not work.
    • 我如何在模式中删除任何 [<什么>]

      How can I delete anything in the pattern [<anything>]?

      推荐答案

      试试这个sed的行:

      sed 's/\[[^]]*\]//g' 
      

      例如:

      kent$  echo "The fish [ate] the bird.
      [This is some] text.
      Here is a number [1001] and another [1201]."|sed 's/\[[^]]*\]//g' 
      The fish  the bird.
       text.
      Here is a number  and another .
      

      说明:

      正则表达式实际上是简单的:

      the regex is actually straightforward:

      \[     #match [
      [^]]*  #match any non "]" chars
      \]     #match ]
      

      因此​​它是

      匹配的字符串,从 [那么所有的字符,但] 结束]

      match string, starting with [ then all chars but ] and ending with ]

      这篇关于如何删除方括号里面和任何文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆