如何提取包含在“$”和“/”(特殊字符)之间的子字符串,然后替换该子字符串的值?
how to extract the sub-string which is enclosed in between '$' and '/' (special characters) and then substitute the value of that sub-string?
我将主字符串作为 str 和需要命名为 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,"$ /"))
它会给出如下错误:
Error in stri_extract_all(p, "$ /") :
you have to specify either regex
, fixed
, coll
, or charclass
c_prop 将是:
键:值
$导演姓名:DF-1C
$DirectorPortName : 端口,DF-1C
$掩蔽视图名称:000197801199,IS_LGLW9062_VIEW
$MaskingInitiatorPortName:启动器端口,IS_LGLW9062_VIEW
$MaskingAssDeviceName :关联设备,IS_LGLW9062_VIEW
$池名称:000197801199,SRP_1
$PoolBoundDevice : 绑定设备,SRP_1
$端口名称:DF-1C:12
$Region_Name : ap-东南-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() 来删除和替换您的 sub-string。这是我的解决方案。
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
我将主字符串作为 str 和需要命名为 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,"$ /"))
它会给出如下错误:
Error in stri_extract_all(p, "$ /") : you have to specify either
regex
,fixed
,coll
, orcharclass
c_prop 将是:
键:值
$导演姓名:DF-1C
$DirectorPortName : 端口,DF-1C
$掩蔽视图名称:000197801199,IS_LGLW9062_VIEW
$MaskingInitiatorPortName:启动器端口,IS_LGLW9062_VIEW
$MaskingAssDeviceName :关联设备,IS_LGLW9062_VIEW
$池名称:000197801199,SRP_1
$PoolBoundDevice : 绑定设备,SRP_1
$端口名称:DF-1C:12
$Region_Name : ap-东南-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() 来删除和替换您的 sub-string。这是我的解决方案。
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