I have the below character string.
"File-2022-03-08-S1-PAR-C-J1-SET"
How can I remove the second and third occurrences of the dash (-) symbol and keep all others? Essentially, I'd like the date (2023-03-08) to read 20230308.
Not sure but do you want result to be something like File-20220308-S1-PAR-C-J1-SET
if yes, then this can be achieved using :
string <- "File-2022-03-08-S1-PAR-C-J1-SET"
modifiedString <- gsub("-(\\d{4})-(\\d{2})-", "-\\1\\2", string)
modifiedString
Using lookarounds.
> gsub(r"{-(?=\d{2}\D)}", "", "File-2022-03-08-S1-PAR-C-J1-SET", perl=TRUE)
[1] "File-20220308-S1-PAR-C-J1-SET"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With