为什么grep不能在单词边界处匹配正斜杠(例如,来自'./foo/bar/baz'; import baz的`/baz`?)? [英] Why doesn't grep match a forward-slash at word boundaries (e.g. `/baz` in `import baz from './foo/bar/baz';`)?
问题描述
在我的项目中,我试图匹配包含"/baz"的文件.因此,我执行以下操作:
In my project, I'm trying to match files that contain "/baz". So, I do the following:
grep -rnw . -e "\/baz"
输出正确匹配以下实例:
The output correctly matches the following instance:
import { baz } from './baz';
但这似乎与这一行不匹配:
But it does not seem to match on this line:
import { baz } from './foo/bar/baz';
但是如果我在"bar\/baz"
上grep,它确实匹配.发生了什么事?
It does match if I grep on "bar\/baz"
, however. What's going on?
推荐答案
在您的grep
调用中放弃-w
自变量.在grep
帮助中:
Ditch the -w
argument from your grep
call. From grep
help:
-w,--word-regexp强制PATTERN只匹配整个单词
-w, --word-regexp force PATTERN to match only whole words
因此,只有当它是一个完整的单词时,它才会与您的模式匹配(由单词边界包围,例如点,空格,行的开头/结尾...)
It will, therefore, match your pattern only if it's a whole word (surrounded by word boundaries such as a dot, whitespace, begin/end of the line...)
这篇关于为什么grep不能在单词边界处匹配正斜杠(例如,来自'./foo/bar/baz'; import baz的`/baz`?)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!