regex - 为什么在使用正则表达式时 grep 会返回不同的结果?

标签 regex linux bash unix grep

> ps -ef | grep cron

root      1052     1  0 04:35 ?        00:00:00 cron
test   4071  4053  0 10:15 pts/0    00:00:00 grep --color=auto cron

> ps -ef | grep cr[o]n

root      1052     1  0 04:35 ?        00:00:00 cron

为什么当搜索词相同时这两个命令返回不同的结果?

最佳答案

因为在第二种情况下,实际运行的进程是(虽然在应用 grep 过滤器后您看不到第二种情况):

root      1052     1  0 04:35 ?        00:00:00 cron
test   4071  4053  0 10:15 pts/0    00:00:00 grep --color=auto cr[o]n

并且 cr[o]ncr[o]n 不匹配,因为 [o] 指定了字符列表 o,而真正的字符串在 o 周围还包含 []

关于regex - 为什么在使用正则表达式时 grep 会返回不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35045139/

相关文章:

Python-正则表达式解析文件

c - 处理需要访问特权端口 < 1024 的应用程序的标准方法是什么?

PHP 和 Xvfb 的使用

linux - 是否有用于检查 ELF 文件的任何 emacs 模式

MYSQL 与脚本的输出不同

c# - 如何根据父 div 中的符号删除它?

regex - Django AttributeError 'tuple'对象没有属性 'regex'

正则表达式匹配json中的键

bash - 以编程方式检测已安装的 Common Lisp 实现

bash - SSH之后将变量从一个Bash脚本传递到另一个