Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching arbitrary number of digits using grep regex

Tags:

regex

grep

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.

like image 616
hdub Avatar asked Jan 14 '23 04:01

hdub


1 Answers

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
like image 190
Tom Cammann Avatar answered Jan 31 '23 08:01

Tom Cammann