A.Naskar A.Naskar - 3 months ago 7
R Question

How to put character or separator before a string in R

I have a string like :

"Father’s Name : ABC NaskarDate of Birth : 18-01-1979Permanent Address: This is the address field for the personContact Numbers : 98413***28Passport Number:PAN Number: AEFXXXXXXXLanguages Known: Tamil, English"


My desired output is :

"|||Father’s Name : ABC Naskar|||Date of Birth : 18-01-1979|||Permanent Address: This is the address field for the person|||Contact Numbers : 98413***28|||Passport Number:|||PAN Number: AEFXXXXXXX|||Languages Known: Tamil, English"


That means I want to add "|||" before some specific strings like Father’s Name,
Date of Birth etc.Thanks

Answer

We are not able to find a general pattern, but based on the string showed, it seems that the ||| separator should be at the start (^) of the string, whereever there is a lower case letter followed by upper case or number followed by upper case, also before PAN and between XXXX and Languages. In that case, a regex lookaround should work.

gsub("(?<=[a-z0-9])(?=[A-Z])|^|(?<=[XXX])(?=Lang)|(?=PAN)", "|||", str1, perl = TRUE)
#[1] "|||Father’s Name : ABC Naskar|||Date of Birth : 18-01-1979|||Permanent Address: This is the address field for the person|||Contact Numbers : 98413***28|||Passport Number:|||PAN Number: AEFXXXXXXX|||Languages Known: Tamil, English"

data

str1 <- "Father’s Name : ABC NaskarDate of Birth : 18-01-1979Permanent Address: This is the address field for the personContact Numbers : 98413***28Passport Number:PAN Number: AEFXXXXXXXLanguages Known: Tamil, English"
Comments