简体   繁体   English

使用grep查找字符串模式

[英]Finding a string pattern using grep

I'm trying to find a certain sequence in the text of several .txt files. 我正在尝试在几个.txt文件的文本中找到特定的顺序。 I am looking for a string that is joined to a 4 digit number. 我正在寻找连接到4位数字的字符串。 eg Watson1990. 例如Watson1990。 I tested the regex using an online tester and it appeared to work, however the expression (or combinations of it) failed to produce an output on my files. 我使用在线测试仪测试了正则表达式,它似乎可以正常工作,但是表达式(或其组合)未能在文件中产生输出。

My regular expression is as follows: 我的正则表达式如下:

egrep '\w*\d{4}' *.txt

However it does not produce any output. 但是,它不会产生任何输出。 Can you tell me what is wrong with this? 你能告诉我这是怎么了吗? I'm using OSX (Snow Leopard). 我正在使用OSX(雪豹)。

Thanks. 谢谢。

The reason why your regular expression doesn't work is that in extended regular expression syntax the token \\d matches the letter d , not a digit. 您的正则表达式不起作用的原因是,在扩展的正则表达式语法中,令牌\\d与字母d匹配,而不是数字。 Use the character class [0-9] instead. 请改用字符类[0-9]

Also \\w matches digits as well as letters so you probably don't want to use it here. \\w还会匹配数字和字母,因此您可能不想在这里使用它。 Use the character class [A-Za-z] to match letters in AZ or az. 使用字符类[A-Za-z]匹配AZ或az中的字母。

I changed the * to a + because presumably you want at least one letter before the number. 我将*更改为+因为您可能希望数字前至少包含一个字母。 The + means "one or more", whereas * means "zero or more". +表示“一个或多个”,而*表示“零个或多个”。

Finally you may wish to consider what should happen if you see a 5 digit number. 最后,您可能希望考虑如果看到5位数字会发生什么情况。 Your regular expression currently accepts it because a 5 digit number starts with a 4 digit number. 您的正则表达式当前接受它,因为5位数字以4位数字开头。

In conclusion, try this: 最后,请尝试以下操作:

egrep '[a-zA-Z]+[0-9]{4}' *.txt

Your regular expression uses Perl, not extended, regex components. 您的正则表达式使用Perl,而不是扩展的regex组件。 Try 尝试

grep -P '\w\d{4}' *.txt

if your version of grep has that option. 如果您的grep版本具有该选项。 I'm using GNU grep 2.5.1 and the -P option is listed as "highly experimental". 我正在使用GNU grep 2.5.1,并且-P选项被列为“高度实验性”。

GNU grep

grep -Po "(\w+\d{4})" file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM