I want to match the other than square brackets characters in regular expression.
我想匹配正则表达式中的方括号字符以外的其他字符。
Precisely I want to match some special characters and some others don't, thus I want to specify them
确切地说,我想匹配一些特殊字符而其他一些不匹配,因此我想指定它们
# grep $'[^a-zA-Z0-9#\\/:!<>{},=?. ()["_+;*\'&|$-]' file
This is missing a ]
, I have tried escaping with \]
, \\]
and so on, I read people doing that outer the [^]
, but I need it inside!
这是缺少一个],我已经尝试用\],\\]等等来逃避,我读到人们在[^]外面做了,但我需要它在里面!
1 个解决方案
#1
8
Place the ]
immediately after the ^
.
在^之后立即放置]。
Here's an example. Input file "foo" contains:
这是一个例子。输入文件“foo”包含:
foo
[
bar
]
baz
quux
We execute the command:
我们执行命令:
grep '[^][]' foo
The output is:
输出是:
foo
bar
baz
quux
From the documentation on bracket expressions in POSIX regular expressions:
从POSIX正则表达式中的括号表达式文档:
The right-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).
右括号(']')将失去其特殊含义,如果它首先出现在列表中(在最初的抑扬符号('^')之后,如果有的话),则表示自己在括号表达式中。
and also:
The special characters '.', '*', '[', and '\' (period, asterisk, left-bracket, and backslash, respectively) shall lose their special meaning within a bracket expression.
特殊字符'。','*','['和'\'(句点,星号,左括号和反斜杠)将在括号表达式中失去其特殊含义。
#1
8
Place the ]
immediately after the ^
.
在^之后立即放置]。
Here's an example. Input file "foo" contains:
这是一个例子。输入文件“foo”包含:
foo
[
bar
]
baz
quux
We execute the command:
我们执行命令:
grep '[^][]' foo
The output is:
输出是:
foo
bar
baz
quux
From the documentation on bracket expressions in POSIX regular expressions:
从POSIX正则表达式中的括号表达式文档:
The right-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).
右括号(']')将失去其特殊含义,如果它首先出现在列表中(在最初的抑扬符号('^')之后,如果有的话),则表示自己在括号表达式中。
and also:
The special characters '.', '*', '[', and '\' (period, asterisk, left-bracket, and backslash, respectively) shall lose their special meaning within a bracket expression.
特殊字符'。','*','['和'\'(句点,星号,左括号和反斜杠)将在括号表达式中失去其特殊含义。