我有一个文件myfile 。 我想找到包含以下模式的行:
MyWebsite/1234或者更一般
somecharactersstringswhitespaces MyWebsite / 987 somecharactersstringswhitespaces
MyWebsite之后可以有一个或多个号码。 我试过以下:
grep ".*MyWebsite[0-9]\{1,\}" myfile grep "MyWebsite[0-9]\{1,\}" myfile grep MyWebsite"[0-9]\{1,\}" myfile grep -E ".*MyWebsite/[0-9]\{1,\}.*" myfile他们都没有工作,即使直觉和逻辑说这应该是好的。
I've got a file myfile. I want to find lines that includes following pattern:
MyWebsite/1234or more general
somecharactersstringswhitespacesMyWebsite/987somecharactersstringswhitespaces
There can be 1 or more numbers after MyWebsite. I have tried following:
grep ".*MyWebsite[0-9]\{1,\}" myfile grep "MyWebsite[0-9]\{1,\}" myfile grep MyWebsite"[0-9]\{1,\}" myfile grep -E ".*MyWebsite/[0-9]\{1,\}.*" myfileNone of them worked, even if intuition and logic says that should be good.
最满意答案
grep 'MyWebsite/[0-9]\+' myfile应该有效。 在你的前3个正则表达式中,你错过了/和最后一个,你不应该逃过{和} ,但是否则你很好。 尽管使用+而不是{1,}更简洁{1,}但grep默认使用的正则表达式版本(POSIX BRE的变体)要求对其进行转义。
grep 'MyWebsite/[0-9]\+' myfile should work. On your first 3 regexes, you missed the / and on the last, you shouldn't have escaped the { and }, but otherwise you were pretty good. It's more concise to use + rather than {1,} though, and the version of regex that grep uses by default (a variant of POSIX BRE) requires it to be escaped.
更多推荐
发布评论