问题描述
\ +稍后
在您的示例文件中,您也有一行:
datafhj893724897290384later
目前由于数据和数字之间存在字母,因此不会匹配。我们可以通过添加一个 [^ 0-9] * 来匹配数据之后的任何数字,直到数字。
我们的最终命令是:
grep'^ [Dd] ata [^ 0-9] * \ d \ +稍后$'文件名
I've got a file that has lines in it that look similar as follows
data datalater 983290842 Data387428later datafhj893724897290384later 4329804928later
What I am looking to do is use regex to match any line that starts with data and ends with later AND has numbers in between. Here is what I've concocted so far:
^[D,d]ata[0-9]*later$
However the output includes all datalater lines. I suppose I could pipe the output and grep -v datalater, but I feel like a single expression should do the trick.
Use + instead of *.
+ matches at least one or more of the preceding.
* matches zero or more.
^[Dd]ata[0-9]+later$
In grep you need to escape the +, and we can use \d which is a character class and matches single digits.
^[Dd]ata\d\+later$
In you example file you also have a line:
datafhj893724897290384later
This currently will not be matched due to there being letters in-between data and the numbers. We can fix this by adding a [^0-9]* to match anything after data until the digits.
Our final command will be:
grep '^[Dd]ata[^0-9]*\d\+later$' filename
这篇关于正则表达式 - 匹配任意数量的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!