Say it's very easy if I want to find something containing lower-case letters and numbers with
produce_text | grep -E '[0-9a-z]'
Brackets are useful to match a set of characters, but what about those that are somewhat special?
If I want to, using brackets, match any character but one of these: a closing bracket ]
, a dash (or hyphen) "-", both slashes /
and \
, a caret ^
, a colon :
.
Will it look like this (I know this doesn't work)?
[^]-/\^:]
Best Answer
To match a literal
]
and a literal-
in a Bracket Expression you'll have to use them like this:or, even better, since some tools require the backslash to be escaped:
that is
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 '^', if any)
and
The hyphen-minus character shall be treated as itself if it occurs first (after an initial '^', if any) or last in the list
hence
If a bracket expression specifies both '-' and ']', the ']' shall be placed first (after the '^', if any) and the '-' last within the bracket expression.
The rules for bracket expressions are the same for ERE and BRE.