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 98 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
- 377,200
- 20
- 156
- 213
-
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
- 70,273
- 8
- 34
- 66