Stringr regex for alpha including accents

Viewed 56
string = "2001 - l'odyssée de l'espace"

What the regex to extract only "l'odyssée de l'espace" with str_exctract()?

str_extract_all(string, '[^-[:digit:]]') would work, but a can't concat it back together.

2 Answers

Here's a base R approach:

trimws(gsub("[-[:digit:]]", "", string))
# [1] "l'odyssée de l'espace"

An imperfect stringr extraction:

stringr::str_extract_all(string, "[^-[:digit:]]+")
# [[1]]
# [1] " "                      " l'odyssée de l'espace"

which can be extended with

grep("\\S", stringr::str_extract_all(string, "[^-[:digit:]]+", simplify = TRUE), value = TRUE)
# [1] " l'odyssée de l'espace"

Another way, to extract what is needed :

sub('\\d+\\s*-\\s*(.*)', '\\1', string)
#[1] "l'odyssée de l'espace"
Related