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
  • 355,584
  • 18
  • 123
  • 178
1

The following regex will do it.

x <- "ABCDx1234xaP_solution"

sub("^.*x([^x]+$)", "\\1", x)
#[1] "aP_solution"
Rui Barradas
  • 57,195
  • 8
  • 29
  • 57