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.
Asked
Active
Viewed 47 times
2 Answers
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
-
1Much better than mine, I forgot that regex's are greedy. – Rui Barradas Apr 02 '20 at 09:26
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