如何在R中的字符串前放置字符或分隔符

How to put character or separator before a string in R

我有一个像这样的字符串:

"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"

我想要的输出是:

"|||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"

这意味着我要添加“|||”在一些特定的字符串之前,比如父亲的名字, 出生日期 etc.Thanks

我们无法找到一般模式,但根据显示的字符串,似乎 ||| 分隔符应该在字符串的开头 (^),无论在哪里是小写字母后跟大写字母或数字后跟大写字母,也在 PAN 之前和 XXXXLanguages 之间。在这种情况下,正则表达式环视应该可以工作。

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"

数据

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"
gsub("(Father’s Name)|(Date of Birth)", "|||\1\2", x)
[1] "|||Father’s Name : ABC Naskar|||Date of Birth : 18-01-1979Permanent Address: This is the address field for the personContact Numbers : 98413***28Passport Number:PAN Number: AEFXXXXXXXLanguages Known: Tamil, English"

幸运的是,正则表达式 OR "|" 能够在第二个模式的替换中分发。