Grep in R (zero or any character)

298 views Asked by At

I have files with a lot of data, but in several of them date is in this format: YYYYMMDD, f. e. 20150704

And in the others date is in this format: YYYY-MM-DD, f. e. 2015-07-04

I want to grep to find specific date, can I do this by one grep to both examples?

I tried this:

grep("*07*04",file)

and this

grep(".07.04",file)

Second form worked for this format YYYY-MM-DD but not for YYYYMMDD

Is there any way to do this by one grep?

3

There are 3 answers

0
akrun On BEST ANSWER

Try

grep('-?07-?04$', str1)

data

 str1 <- c('2015-07-04', '20150704', '2014-08-07', '20150407',
          '2015-07-14', '2015-01-04')
2
Aaron Katch On

Another solution is to use gsub first:

str1 <- c('2015-07-04', '20150704', '2014-08-07', '20150407',
       '2015-07-14', '2015-01-04')

grep('*0704',gsub("-","",str1))
3
IRTFM On

This seems fairly simple:

grep('07-*04',str1)

The asterisk quantifier says "zero or more times" for the character or group immediately to its left. And fortunately dash is not a special regex symbol. If it were you would either need to use square-brackets to make a character class or doubly escape. (I got the idea that you thought the asterisk was a wildcard and that's not the case.)