Use stringr to split string by capital letter but not USA

This is a StackOverflow answer I'm trying to implement, but that global $1 doesn't seem to work.

library(stringr)

str_vec <- c("TodayILiveInTheUSAWithSimon", "USAToday", "IAmSOOOBored")

stringr::str_replace_all(str_vev,  "((?<=[a-z])[A-Z]|[A-Z](?=[a-z]))",  " $1")

I can't make stringr yield:

 Today I Live In The USA With Simon
USA Today
I Am SOOO Bored

Hi there,

I also often get confused with the different back-reference symbols used for capture groups. Some languages use $1, others \1. R uses the latter but you need to escape the backslash for it to work properly:

library(stringr)

str_vec <- c("TodayILiveInTheUSAWithSimon", "USAToday", "IAmSOOOBored")

str_replace_all(str_vec,  "((?<=[a-z])[A-Z]|[A-Z](?=[a-z]))",  " \\1")
#> [1] " Today I Live In The USA With Simon" "USA Today"                          
#> [3] "I Am SOOO Bored"

Created on 2021-11-12 by the reprex package (v2.0.1)

Hope this helps,
PJ

1 Like

Excellent.. Thank you! I didn't know about the two conventions.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.