Split a string with numbers and letters

I have lines with letters and numbers. I would like to separate them and make an offer from them.

a<-"DiabetesTestInPast12months"
b<-"SmokingMorethan12PackYears"
c<-"30MinsOrLessExercise"

I would like to receive:

a<-"Diabetes test in past 12 months"
b<-"Smoking more than 12 pack years"
c<-"30 mins or less exercise"

I could not find a way to extract the number inside the vector using stringr str_extract_all.

+4
source share
2 answers

I would try:

#combine all the string in a vector
a<-c(a,b,c)
gsub("(?<=[0-9])(?=[A-Za-z])","\\1 \\2",
     gsub("(?<=[a-z])(?=[A-Z0-9])","\\1 \\2",a,perl=TRUE),
     perl=TRUE)
#[1] "Diabetes Test In Past 12 months" "Smoking Morethan 12 Pack Years" 
#[3] "30 Mins Or Less Exercise"

Simplification a bit:

gsub("(?<=[a-z])(?=[A-Z0-9])|(?<=[0-9])(?=[A-Za-z])"," ",a,perl=TRUE)

gets the same result.

Please note that it is Morethanimpossible to break, because there is no way to know that these are separate words ( Morethanwould).

+5
source

I am working on C #. Use the code below so you can split the string according to your requirement.

       `if (str.IsAllUpper()) return str;
        return Regex.Replace(str, "([A-Z]{1,2}|[0-9]+)", " $1").TrimStart();`
0
source

Source: https://habr.com/ru/post/1626639/


All Articles