str_extract specific patterns (example)

前端 未结 4 1863
轻奢々
轻奢々 2021-01-06 12:04

I\'m still a little confused by regex syntax. Can you please help me with these patterns:

_A00_A1234B_
_A00_A12345B_
_A1_A12345_

my approac

4条回答
  •  死守一世寂寞
    2021-01-06 12:58

    You can do this without using a regular expression ...

    x <- c('_A00_A1234B_', '_A00_A12345B_', '_A1_A12345_')
    sapply(strsplit(x, '_', fixed=T), '[', 3)
    # [1] "A1234B"  "A12345B" "A12345" 
    

    If you insist on using a regular expression, the following will suffice.

    regmatches(x, regexpr('[^_]+(?=_$)', x, perl=T))
    

提交回复
热议问题