Skip to content

Instantly share code, notes, and snippets.

@infotroph
Created February 14, 2016 23:34
Show Gist options
  • Save infotroph/7609f1a0bca9aa157020 to your computer and use it in GitHub Desktop.
Save infotroph/7609f1a0bca9aa157020 to your computer and use it in GitHub Desktop.
strs = c("nomatch", "Odum 1969", "2001FACE_3b", "1800", "sdfghj1928qwer")
strs_yrs = gsub(".*((19|20)[0-9]{2}).*", "\\1", strs)
strs_yrs
# [1] "nomatch" "1969" "2001" "1800" "1928"
# Oops, strings with no year (or year from wrong century) are returned unchanged! Let's remove them separately:
strs_haveyr = grepl("(19|20)[0-9]{2}", strs)
strs_yrs[!strs_haveyr] = ""
strs_yrs
# [1] "" "1969" "2001" "" "1928"
# (N.B. assumes only one year per string -- if several, will return only 1st one.)
@davidjpmoore
Copy link

Thanks for this! I'm just trying it now.
*** This works great *** Just saved me going through a long list of differently formatted references to extract the year of publication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment