如何将字符或分隔符放在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"

这意味着我想在一些特定字符串之前添加"|||",比如"父亲的名字",出生日期等感谢

我们无法找到一个通用模式,但根据显示的字符串,似乎|||分隔符应该在字符串的开头(^(,其中有一个小写字母后跟大写字母或数字后跟大写字母,也在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 "|"能够在第二种模式的替换中分布。

相关内容

  • 没有找到相关文章

最新更新