Home > database >  How to extract date from a vector using stringr functions
How to extract date from a vector using stringr functions

Time:01-13

I have the following vector.

vec <- c("1","/publications/coronavirus-covid-19-update-first-ministers-statement-11-january-2022/",
  "2","/publications/coronavirus-covid-19-update-first-ministers-statement-3-august-2021-1/",
  "3","/publications/coronavirus-covid-19-update-first-ministers-speech-27-july-2021/",
  "4","/publications/coronavirus-covid-19-update-first-ministers-statement-20-july-2021/",
  "5","/publications/coronavirus-covid-19-update-first-ministers-statement-15-june-2021/")

I would like to extract dates so that I obtain the following;

[1] "3-august-2021" "27-july-2021"  "20-july-2021"  "15-june-2021"

CodePudding user response:

If you do not want to specify the month names, you can use,

\d{1,2}-\w -\d{4}

CodePudding user response:

You can use this regex:

\d{1,2}-(?:january|february|march|april|may|june|july|august|september|october|november|december)-\d{4}
  •  Tags:  
  • Related