0

I have a column in which i want to extract characters which are before x from right hand side. Sample strings in column is ABCDx1234xaP_solution. I need to extract aP_solution.

2 Answers2

2

A simple solution using sub could be to remove everything until last 'x'.

sub('.*x', '', 'ABCDx1234xaP_solution')
#[1] "aP_solution"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

The following regex will do it.

x <- "ABCDx1234xaP_solution"

sub("^.*x([^x]+$)", "\\1", x)
#[1] "aP_solution"
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66