gsub to extract string before and after dots from a vector in R?

Asked By: Anonymous

Please, I want to use gsub to extract strings from this vector:

x<-("Prayer: Lord. Have mercy on.")

The desired output is "Lord" and "Have mercy on" separately.

I tried gsub('.*:(.*)','\1',x) but it doesn’t give them separately.


Answered By: Anonymous

If you need to get these two values separately, you can use

x <- c("Prayer: Lord. Have mercy on.")
## => [1] "Lord"
## => [1] "Have mercy on"

See the R demo online, regex #1 and regex #2 demos. It does not matter if you use sub or gsub with these regexps, they will work the same, although sub is more logical as all you need is replace the whole string with the value of the first capturing group.


  • ^ – start of string
  • [^:]* – zero or more chars other than :
  • : – a colon
  • s* – zero or more whitespaces
  • [^.]+ – one or more chars other than a dot
  • . – a dot
  • s* – zero or more whitespaces
  • ([^.]+)Capturing group 1: one or more chars other than dots
  • .* – the rest of the string.
techinplanet staff

techinplanet staff

Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno