Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get String Before Last occurrence of substring?

I want to get String before last occurrence of my given sub string.

My String was,

path = D:/me/vol101/Prod/cent/2019_04_23_01/image/AVEN_000_3400_img_pic_p1001-1010/pxy/AVEN_000_3400_img-mp4_to_MOV_v1001-1010.mov

my substring, 1001-1010 which will occurred twice. all i want is get string before its last occurrence.

Note: My substring is dynamic with different padding but only number.

I want,

D:/me/vol101/Prod/cent/2019_04_23_01/image/AVEN_000_3400_img_pic_p1001-1010/pxy/AVEN_000_3400_img-mp4_to_MOV_v

I have done using regex and slicing,

>>> p = 'D:/me/vol101/Prod/cent/2019_04_23_01/image/AVEN_000_3400_img_pic_p1001-1010/pxy/AVEN_000_3400_img-mp4_to_MOV_v1001-1010.mov'
>>> q = re.findall("\d*-\d*",p)
>>> q[-1].join(p.split(q[-1])[:-1])
'D:/me/vol101/Prod/cent/2019_04_23_01/image/AVEN_000_3400_img_pic_p1001-1010/pxy/AVEN_000_3400_img-mp4_to_MOV_v'
>>> 

Is their any better way to do by purely using regex?

Please Note I have tried so many eg:

  1. regular expression to match everything until the last occurrence of /
  2. Regex Last occurrence?

I got answer by using regex with slicing but i want to achieve by using regex alone..

like image 419
Mohideen bin Mohammed Avatar asked Jan 02 '23 01:01

Mohideen bin Mohammed


2 Answers

Why use regex. Just use built in string methods:

path = "D:/me/vol101/Prod/cent/2019_04_23_01/image/AVEN_000_3400_img_pic_p1001-1010/pxy/AVEN_000_3400_img-mp4_to_MOV_v1001-1010.mov"
index = path.rfind("1001-1010")
print(path[:index])
like image 173
Error - Syntactical Remorse Avatar answered Jan 08 '23 02:01

Error - Syntactical Remorse


You can use a simple greedy match and a capture group:

(.*)1001-1010

Your match is in capture group #1

Since .* is greedy by nature, it will match longest match before matching your keyword 1001-1010.

RegEx Demo


As per comments below if keyword is not a static string then you may use this regex:

r'(.*\D)\d+-\d+'

Python Code:

>>> p = 'D:/me/vol101/Prod/cent/2019_04_23_01/image/AVEN_000_3400_img_pic_p1001-1010/pxy/AVEN_000_3400_img-mp4_to_MOV_v1001-1010.mov'
>>> print (re.findall(r'(.*\D)\d+-\d+', p))
['D:/me/vol101/Prod/cent/2019_04_23_01/image/AVEN_000_3400_img_pic_p1001-1010/pxy/AVEN_000_3400_img-mp4_to_MOV_v']
like image 30
anubhava Avatar answered Jan 08 '23 01:01

anubhava