今天被這個方括號懵暈了,特此記錄
例如: 去除輸入字符串“1[2.3]4[ab,c]”中的所有方括號和逗號:
$ echo "1[2.3]4[ab,c]"|sed -e "s/[,\]\[]//g"
1[2.3]4[ab,c]
?
It doesn't work!
原因:Regular Expressions
The <right-square-bracket> (?']'?) shall lose its special meaning and represent itself in a bracket expression if it occurs first in the list (after an initial <circumflex> (?'^'?), if any).
解決方案:關(guān)鍵是要把 ] 右方括號不加escape放在首位.
$ echo "1[2.3]4[ab,c]"|sed -e "s/[][,]//g"
12.34abc
$ echo "1[2.3]4[ab,c]"|sed -e "s/[]\[,]//g"
12.34abc
$ echo "1[2.3]4[ab,c]"|sed -e "s/[],[]//g"
12.34abc
?
The order of some characters is important:
-
-
?should be at the end like this?-]
-
[]
?should be like that?[][other characters]
-
'
?should be escaped like that?'\''
- not begin with?
^
?like in?[^
- not begin with?
[.
?[=
?[:
?and end with?.]
?=]
?:]
- not end with?
$]
References:
regex - How to escape square closing bracket in sed - Stack Overflow文章來源:http://www.zghlxwxcb.cn/news/detail-633937.html
Regular Expressions文章來源地址http://www.zghlxwxcb.cn/news/detail-633937.html
到了這里,關(guān)于SED正則表達式中[方括號]的特殊處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!