sed regex:从字符串中删除[和]的出现

时间:2015-06-16 10:15:50

标签: regex bash sed

我正在使用Linux Mint 17。

我可以替换除[]之外的测试字符串中的所有内容(打开和关闭方括号)。

这是我的表达式,它位于bash脚本中,我从命令行

运行
video_title=$(echo $video_title | sed 's|[?![]]||g')

我尝试在两个方括号前放置\但这不起作用。 如果我删除了[],则表达式会替换?!就好了。

任何想法?

3 个答案:

答案 0 :(得分:2)

根据手册,要在列表中包含],它必须是第一个字符。

 A leading `^' reverses the meaning of LIST, so that it matches any
 single character _not_ in LIST.  To include `]' in the list, make
 it the first character (after the `^' if needed), to include `-'
 in the list, make it the first or last; to include `^' put it
 after the first character.

所以尝试这样的事情:

$ echo '[!2015?]' | sed 's|[][?!]||g'
2015

答案 1 :(得分:1)

仅从输入DateTime.TryParse(str, CultureInfo.InvariantCulture, DateTimeStyles.None, dt) 中删除字符是比tr更合适的工具。在您的情况下,您可以使用

sed

答案 2 :(得分:0)

你可以试试这个,

$ echo 'foo?![bar]' | sed 's~[?!]\|\]\|\[~~g'
foobar