我们可以搜索“\ w \ w \ w \ d”这样的模式,它代表三个字母,后跟grep中的数字吗?它不起作用。在linux终端上有没有办法做同样的事情?
例如我想匹配'ABC9'或'NMJ6'等
答案 0 :(得分:3)
例如:
echo ABC9 | grep -E '[[:alpha:]]{3}[[:digit:]]' -
这使用man grep
定义的字符类:
Finally, certain named classes of characters are predefined
within bracket expressions, as follows. Their names are self
explanatory, and they are [:alnum:], [:alpha:], [:cntrl:],
[:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:],
[:upper:], and [:xdigit:]. For example, [[:alnum:]] means
[0-9A-Za-z], except the latter form depends upon the C locale and
the ASCII character encoding, whereas the former is independent
of locale and character set. (Note that the brackets in these
class names are part of the symbolic names, and must be included
in addition to the brackets delimiting the bracket expression.)
Most meta-characters lose their special meaning inside bracket
expressions. To include a literal ] place it first in the list.
Similarly, to include a literal ^ place it anywhere but first.
Finally, to include a literal - place it last.
答案 1 :(得分:2)
grep -P“\ w \ w \ w \ d”
-P, --perl-regexp
Interpret PATTERN as a Perl regular expression.
答案 2 :(得分:2)
grep -P '\w\w\w\d'
添加--color使其真正脱颖而出。 例如:
echo 'blahblahABC9blahblah' | grep --color -P '\w\w\w\d'