I have this string: "ABCd1234"
I am trying to extract it to 2 different string according to this pattern:
pattern = "((^[A-Z])([a-z]$))(\d{4})"
str_extract(str, pattern)
it doesn't work. any help?
I have this string: "ABCd1234"
I am trying to extract it to 2 different string according to this pattern:
pattern = "((^[A-Z])([a-z]$))(\d{4})"
str_extract(str, pattern)
it doesn't work. any help?
Remove the EOL anchor ($
) from the pattern, and note that character classes in R are escaped as follows:
library(tidyverse)
x_chr <- c("ABCd1234")
patt <- '(^[[:alpha:]]*)(\\d{4}$)'
x_chr %>%
str_replace(patt, '\\1 \\2') %>%
str_split(' ')
#> [[1]]
#> [1] "ABCd" "1234"
If your data is in a tibble, you can also use tidyr::extract()
. Cheers!
tibble(string = x_chr) %>%
extract(col = string,
into = c('prefix', 'suffix'),
regex = patt)
#> # A tibble: 1 x 2
#> prefix suffix
#> <chr> <chr>
#> 1 ABCd 1234
Created on 2019-06-18 by the reprex package (v0.3.0.9000)
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.