[英]How to get String Before Last occurrence of substring?
I want to get String before last occurrence of my given sub string. 我想在给定子字符串的最后一次出现之前获取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 路径= 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. 我的子字符串1001-1010
将会出现两次。 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 d:/我/ vol101 /生产/分/ 2019_04_23_01 /图像/ 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: 请注意,我尝试了很多,例如:
I got answer by using regex with slicing but i want to achieve by using regex alone.. 我通过切片使用正则表达式得到了答案,但我想单独使用正则表达式来实现。
Why use regex
. 为什么使用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])
You can use a simple greedy match and a capture group: 您可以使用简单的贪婪匹配和捕获组:
(.*)1001-1010
Your match is in capture group #1 您的比赛在捕获组#1中
Since .*
is greedy by nature, it will match longest match before matching your keyword 1001-1010
. 由于.*
本质上是贪婪的,因此在匹配关键字1001-1010
之前,它将匹配最长匹配项 。
As per comments below if keyword is not a static string then you may use this regex: 根据下面的注释,如果关键字不是静态字符串,则可以使用此正则表达式:
r'(.*\D)\d+-\d+'
Python Code: Python代码:
>>> 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']
Thanks @anubhava, 谢谢@anubhava,
My first regex was, 我的第一个正则表达式是
.*(\d*-\d*)\/
Now i have corrected mine.. 现在我更正了我的..
.*(\d*-\d*)
or 要么
(.*)(\d*-\d*)
which gives me, 这给了我
>>> q = re.search('.+(\d*-\d*)', p)
>>> q.group()
'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_v0001-1001'
>>>
(.*\D)\d+-\d+
this gives me exactly what i want... 这给了我我想要的...
>>> q = re.search('(.*\D)\d+-\d+', p)
>>> q.groups()
('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',)
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.