匹配字符串和一个或多个数字与grep(Match string and one or more numbers with grep)

编程入门 行业动态 更新时间:2024-10-11 07:31:07
匹配字符串和一个或多个数字与grep(Match string and one or more numbers with grep)

我有一个文件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/1234

or 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,\}.*" myfile

None 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.

更多推荐

本文发布于:2023-04-29 03:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334416.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   字符串   数字   grep   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!