I have the main string as str and substring which is required named as 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"
and the value of $Region_Name = ap-southeast-1 that is in some other file.
I tried :
r <- unlist(stri_extract_all(p,"$ /"))
and it will give an error like:
Error in stri_extract_all(p, "$ /") : you have to specify either
regex
,fixed
,coll
, orcharclass
c_prop will be:
Key: Value
$DirectorName : DF-1C
$DirectorPortName : Ports, DF-1C
$MaskingViewName : 000197801199, IS_LGLW9062_VIEW
$MaskingInitiatorPortName : Initiator Ports, IS_LGLW9062_VIEW
$MaskingAssDeviceName :Associated Devices, IS_LGLW9062_VIEW
$PoolName : 000197801199, SRP_1
$PoolBoundDevice : Bound Devices, SRP_1
$PortName : DF-1C:12
$Region_Name : ap-southeast-1
How to solve this issue, suggest some idea? Thanks in Advance!!!
This works for your example, does it solve your problem? When using regex (or regular expressions), you have to escape special characters in R with two backslashes \\
. It looks like using stringi
the replacement must have special characters escaped as well, but I do not use stringi
often so hopefully someone can chime in on a better way to do it using 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/"
EDIT: if you want a multiple replacements for the same substring:
# 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"