regex - return all before the second occurrence

前端 未结 4 1430
天命终不由人
天命终不由人 2020-12-08 11:17

Given this string:

DNS000001320_309.0/121.0_t0

How would I return everything before the second occurrence of \"_\"?

DNS0000         


        
4条回答
  •  孤街浪徒
    2020-12-08 12:06

    I think this might do the task (regex to match everything befor the last occurence of _):

    _([^_]*)$
    

    E.g.:

    > sub('_([^_]*)$', '', "DNS000001320_309.0/121.0_t0")
    [1] "DNS000001320_309.0/121.0"
    

提交回复
热议问题