#Replace the first occurrence of a character within a string in R [duplicate]
#sub and gsub perform replacement of the first and all matches respectively.

x = "Request(123): \n Element1: 123123 \n Element2: 456456"

## match everything up through last colon
#".*:" will match and replace everything up through the last colon
gsub(".*:", "", x)
## [1] " 456456"
sub(".*:", "", x)
## [1] " 456456"
## not greedy, match everything up through first colon
#using sub and ? to make * not greedy will work.
sub(".*?:", "", x)
## [1] " \n Element1: 123123 \n Element2: 456456"
## match first colon only
## since we don't need regex here, fixed = TRUE will speed things up
sub(":", "", x, fixed = TRUE)
## [1] "Request(123) \n Element1: 123123 \n Element2: 456456"
## compare to gsub, match every colon
gsub(":", "", x, fixed = TRUE)
## [1] "Request(123) \n Element1 123123 \n Element2 456456"
#reference and acknowledgement
#https://stackoverflow.com/questions/57064509/replace-the-first-occurrence-of-a-character-within-a-string-in-r