user3664020
user3664020

Reputation: 3020

How do I determine if a given string has a date or time in it?

I want to check if a given string has a date or time in it. For example

1) "dfdsf 2014/01/02 dsfdsf"
2) "fgfffdbf 2014-01-02 dfsdfdfd"
3) "dfdfds 02/01/2014"
4) "02/2014/01 dffsdfsd"
5) "2014/01/02 00:30 sdgfsdgsdgvbds"   
6) "02/2014/01 12:20:22 xcbfdgfdg"
7) "01:11:22 02/2014/01 sdgsdgs"
8) "00:30 2014-01-02 fdgfdgd"

All these strings contain date or time in it. I want to build a test/function which tags all these 8 strings as "Date/Time".

Upvotes: 0

Views: 1777

Answers (1)

Sam Danziger
Sam Danziger

Reputation: 106

I would use 'grepl', but there will be a problem if there's a string with something in it that looks like a date, but isn't. Try the example below:

strings <- c("dfdsf 2014/01/02 dsfdsf", "fgfffdbf 2014-01-02 dfsdfdfd", "dfdfds 02/01/2014", "02/2014/01 dffsdfsd", "2014/01/02 00:30 sdgfsdgsdgvbds",  "02/2014/01 12:20:22 xcbfdgfdg", "01:11:22 02/2014/01 sdgsdgs", "00:30 2014-01-02 fdgfdgd", "NoDateHere")


findDates <- function(strings) {
    pattern1 <- '[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]'
    pattern2 <- '[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]'
    pattern3 <- '[0-9][0-9]/[0-9][0-9][0-9][0-9]/[0-9][0-9]'
    pattern4 <- '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]'

    tdBool <- grepl(pattern1, strings) | grepl(pattern2, strings) | grepl(pattern3, strings) | grepl(pattern4, strings)
    return(tdBool)
}

findDates(strings)

Upvotes: 4

Related Questions