我有主字符串作为str和substring,需要命名为su_str:
su_str =“$ Region_Name”,
str <- " https://lglbw.pqr.xyz.com:58443/APG/lookup/All/Report%20Library/Amazon%20S3/Inventory/Regions/$Region_Name/Billing/report.csv"
以及其他文件中$ Region_Name = ap-southeast-1的值。
我试过了 :
r <- unlist(stri_extract_all(p,"$ /"))
它会给出一个错误,如:
stri_extract_all(p,“$ /”)出错:你必须指定
regex
,fixed
,coll
或charclass
c_prop将是: 核心价值 $ DirectorName:DF-1C $ DirectorPortName:Ports,DF-1C $ MaskingViewName:000197801199,IS_LGLW9062_VIEW $ MaskingInitiatorPortName:启动器端口,IS_LGLW9062_VIEW $ MaskingAssDeviceName:相关设备,IS_LGLW9062_VIEW $ PoolName:000197801199,SRP_1 $ PoolBoundDevice:绑定设备,SRP_1 $ PortName:DF-1C:12 $ Region_Name:ap-southeast-1
如何解决这个问题,提出一些想法?提前致谢!!!
这适用于您的示例,它是否解决了您的问题?使用正则表达式(或正则表达式)时,必须使用两个反斜杠\\
来转义R中的特殊字符。它看起来像使用stringi
替换必须有特殊字符转义,但我不经常使用stringi
所以希望有人可以使用stringi
更好的方式来实现它
> library(stringi)
>
> str <- " https://lglbw.pqr.xyz.com:58443/APG/lookup/All/Report%20Library/Amazon%20S3/Inventory/Regions/$Region_Name/Billing/report.csv"
>
> # If you just want to extract the sequence of letters and underscores's after "$" and before the "/"
> unlist(stri_extract_all(str, regex = "\\$[[:alpha:]_]*\\b"))
[1] "$Region_Name"
>
> # If you want to replace it with something else using base R
>
> some_string <- "$Region_Name = ap-southeast-1"
>
> gsub("\\$[[:alpha:]_]*\\b", some_string, str)
[1] " https://lglbw.pqr.xyz.com:58443/APG/lookup/All/Report%20Library/Amazon%20S3/Inventory/Regions/$Region_Name = ap-southeast-1/Billing/report.csv"
>
> # Using stringi package
>
> # Special characters have to be escaped
> some_string <- "\\$Region_Name \\= ap\\-southeast\\-1"
>
> stri_replace_all(str, some_string, regex = "\\$[[:alpha:]_]*\\b")
[1] " https://lglbw.pqr.xyz.com:58443/APG/lookup/All/Report%20Library/Amazon%20S3/Inventory/Regions/$Region_Name = ap-southeast-1/"
编辑:如果你想要同一个子串的多个替换:
# If the substring will always be "$Region_Name"
su_str <- "$Region_Name"
replacements <- c("$Region_Name = ap-southeast-1/", "$Region_Name = ap-southeast-2/")
stri_replace_all(str, replacements, fixed = su_str)
[1] " https://lglbw.pqr.xyz.com:58443/APG/lookup/All/Report%20Library/Amazon%20S3/Inventory/Regions/$Region_Name = ap-southeast-1//Billing/report.csv"
[2] " https://lglbw.pqr.xyz.com:58443/APG/lookup/All/Report%20Library/Amazon%20S3/Inventory/Regions/$Region_Name = ap-southeast-2//Billing/report.csv"
你问题的标题和你要问的是两个不同的问题,但我会尝试解决它们。
关于使用stri_extract_all()获得的错误,您需要指定要匹配的模式类型,我相信您正在尝试匹配固定模式,在这种情况下您可以使用
stri_extract_all_fixed()
功能而不是。
但是,我不使用stri_extract_all()来删除和替换您的子字符串。这是我的解决方案。
str <- " https://lglbw.pqr.xyz.com:58443/APG/lookup/All/Report%20Library/Amazon%20S3/Inventory/Regions/$Region_Name/Billing/report.csv"
reg<-"$Region_Name"
replce<-"ap-southeast-1"
# Custom function to return position of a sub string
strpos_fixed<-function(x,y){
a<-regexpr(y, x,fixed=T)
b<-a[1]
return(b)
}
part1<-substr(str,1,(strpos_fixed(str,reg)-1))
part2<-substr(str,(strpos_fixed(str,reg)+nchar(reg)),nchar(str))
part1 # Everything before "$Region_Name"
part2 # Everything after "$Region_Name"
new<-paste(part1,replce,part2, sep ="")
new