I have been trying to get this right. What I want to do is extract a year from a string. The string looks like this for example:
Toy Story (1995)
Twelve Monkeys (a.k.a. 12 Monkeys) (1995)
year = gsub("(?<=\\()[^()]*(?=\\))(*SKIP)(*F)|.", "", x, perl=T)
[1] 1995
[2] a.k.a. 12 Monkeys1995
We can use
library(stringr)
as.numeric(str_extract(x, "(?<=\\()[0-9]+(?=\\))"))
#[1] 1995 1995
x <- c("Toy Story (1995)", "Twelve Monkeys (a.k.a. 12 Monkeys) (1995)")