简体   繁体   English

如何在最后一次出现子字符串之前获取字符串?

[英]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: 请注意,我尝试了很多,例如:

  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.. 我通过切片使用正则表达式得到了答案,但我想单独使用正则表达式来实现。

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之前,它将匹配最长匹配项

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: 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.

相关问题 尝试使用 python 中的递归在字符串中最后一次出现 substring - Trying to get the last occurrence of a substring in string using recursion in python 查找字符串中最后一次出现的子字符串,替换它 - Finding last occurrence of substring in string, replacing that 查找字符串中子字符串最后一次出现的索引 - Find index of last occurrence of a substring in a string 获取字符串中的最后一个子字符串? - Get the last substring in string? 在第一次出现之前提取 substring,在 Pyspark 中最后一次出现定界符之后提取 substring - extract substring before first occurrence and substring after last occurrence of a delimiter in Pyspark 替换字符串中最后一次出现的子字符串的方法更快? - Which method is faster for replacing the last occurrence of a substring in a string? 在第二次出现没有字符串切片的字符之前选择一个子字符串 - Select a substring before the second occurrence of a character without a string slice 如何在第一次出现数字之前的最后一个字母处截断字符串? - How do I truncate a string at the last letter before the first occurrence of a digit? 从字符串中获取所有匹配字符的 substring 的出现 - get occurrence of all substring of matching characters from string 通过在python中另一个字符之前最后出现的字符来解析字符串 - parse string by the last occurrence of the character before another character in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM