Replacing alphanumeric string in R

I have a dataset with alphanumeric strings. I am trying to replace the numeric part of the string with "nnnn" and the alphabet part with "aaaa". The length of the alphabet and numeric part does not matter.

Example: What I am expecting
#100001032218888example1 #nnnnaaaannnn
#1-example-2 #nnnn-aaaa-nnnn

I tried this:

  mutate(str_alpha = str_replace_all(col_name, "\\w+", "aaaa"),
         str_num_alpha = str_replace_all(str_alpha, "\\d+", "nnnn"))

I think I got the "\\w+" incorrect because it replaces the numeric part of the string too.

Can someone please help? Thank you.

>Solution :

Do the reverse i.e. replace first the alphabets and then the digits so

library(stringr)
str_replace_all(str_replace_all(str1, "[[:alpha:]]+", "aaaa"), "\\d+", "nnnn")

-output

[1] "#nnnnaaaannnn"   "#nnnn-aaaa-nnnn"

Or using a named vector in a single str_replace_all

str_replace_all(str1, setNames(c("aaaa", "nnnn"), c("[[:alpha:]]+", "\\d+")))
[1] "#nnnnaaaannnn"   "#nnnn-aaaa-nnnn"

data

str1 <- c("#100001032218888example1", "#1-example-2")

Leave a Reply