How to Replace the string inside the semicolon and the comma to "X" using R regex.
Input:
My name : Harry, Age : 23, Address : London,
Output:
My name : X, Age : X, Address : X,
How to Replace the string inside the semicolon and the comma to "X" using R regex.
Input:
My name : Harry, Age : 23, Address : London,
Output:
My name : X, Age : X, Address : X,
gsub(": .*?,", ": X,", "My name : Harry, Age : 23, Address : London,")
#[1] "My name : X, Age : X, Address : X,"
you can use gsub
gsub( "[a-zA-Z0-9]+[,]",": X, ","My name : Harry, Age : 23, Address : Londo")
You can use the gsub
, This performs greedy search substitution.
gsub(':[^,]+,',': X,',"My name : Harry, Age : 23, Address : London,")
## [1] "My name : X, Age : X, Address : X,"
The regex will look for sequences that match the discription in the first argument.
[^,]+
matches to a sequence that contains no commas, this ensures that only the next comma can be considered as part of the overall sequence.