Regex matching everything that's not a 4 digit number
r, regex
Solution
It's possible to capture group in regex using `()`. Taking the same example
str12 <- "coihr 1234 &/()= jngm 34 ljd"
gsub(".*\\s(\\d{4})\\s.*", "\\1", str12)
[1] "1234"
Problem
I match and replace 4-digit numbers preceded and followed by white space with: ``` str12 <- "coihr 1234 &/()= jngm 34 ljd" sub("\\s\\d{4}\\s", "", str12) [1] "coihr&/()= jngm 34 ljd" ``` but, every try to invert this and extract the number instead fails. I want: ``` [1] 1234 ``` does someone has a clue? ps: I know how to do it with {stringr} but am wondering if it's possible with {base} only.. ``` require(stringr) gsub("\\s", "", str_extract(str12, "\\s\\d{4}\\s")) [1] "1234" ```