简体   繁体   English

Android Shell sed麻烦

[英]Android shell sed trouble

I'm trying to use sed to remove all instances of text enclosed by square brackets from a document, so my code is: 我正在尝试使用sed从文档中删除所有用方括号括起来的文本实例,因此我的代码是:

sed -r 's|\[[^\]]*\]||g' file

However, this does not seem to pick up all instances of text enclosed by square brackets. 但是,这似乎并不代表方括号括起来的所有文本实例。 In particular, it seems to fail with: 特别是,它似乎失败了:

[/a]
[/b]
[span style "blah" blah]

and so forth, but it works with square brackets enclosing plaintext. 依此类推,但适用于包含纯文本的方括号。 As far as I can tell, the regex should be looking for square brackets enclosing any character that is not a ], so what exactly is wrong? 据我所知,正则表达式应该在寻找方括号括起来的不是[]的任何字符,那么到底是什么错呢?

I tried this regex and it didn't remove any text in brackets. 我尝试了此正则表达式,但没有删除括号中的任何文本。

When I removed that second '\\', so the regex was 当我删除第二个“ \\”时,正则表达式为

 sed -r 's|\[[^]]*\]||g'

it worked perfectly in all cases. 它在所有情况下都能完美运行。

I tried it on both Mac and Linux. 我在Mac和Linux上都尝试过。 Android runs Linux, so it will probably work there too. Android运行Linux,因此它也可能在其中运行。


A word of explanation: Scansets are special where escapes are concerned. 一句话解释:扫描集在涉及转义的地方很特殊。 If the ']' character is the very first one in the scanset, you don't need to escape it. 如果']'字符是扫描集中的第一个字符,则无需转义它。 If it's not the very first one in the scanset, you can't escape it. 如果它不是扫描集中的第一个, 则无法转义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM