获取字符串中的最后一个子字符串?

时间:2014-11-24 11:20:42

标签: python string substring

[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:638638 t:10.643967 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:640640 t:10.677333 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:642642 t:10.710700 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:644644 t:10.744067 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:646646 t:10.777433 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:648648 t:10.810800 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:650650 t:10.844167 crop=400:704:440:8
frame=  326 fps=0.0 q=0.0 Lsize=N/A time=00:00:10.89 bitrate=N/A    
video:31kB audio:1876kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

你怎么会得到最后一个子串“crop = 400:704:440:8”字符串?我能想到的是使用rfind(“crop =”)获取初始索引,但我不确定接下来会做什么?

我的解决方案:

start =  output.rfind("crop=")
end =  output.find('\n', start, len(output))

print output[start:end]

6 个答案:

答案 0 :(得分:1)

您可以尝试以下re.findall功能。

>>> import re
>>> s = """[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:638638 t:10.643967 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:640640 t:10.677333 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:642642 t:10.710700 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:644644 t:10.744067 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:646646 t:10.777433 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:648648 t:10.810800 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:650650 t:10.844167 crop=400:704:440:8
frame=  326 fps=0.0 q=0.0 Lsize=N/A time=00:00:10.89 bitrate=N/A    
video:31kB audio:1876kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown"""
>>> re.findall(r'crop=[\d:]+', s)[-1]
'crop=400:704:440:8'

答案 1 :(得分:1)

您只需使用str.splitstr.startswith进行索引即可。

a = """[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:638638 t:10.643967 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:640640 t:10.677333 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:642642 t:10.710700 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:644644 t:10.744067 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:646646 t:10.777433 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:648648 t:10.810800 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:650650 t:10.844167 crop=400:704:440:8
frame=  326 fps=0.0 q=0.0 Lsize=N/A time=00:00:10.89 bitrate=N/A    
video:31kB audio:1876kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown"""

for line in a.split('\n'):
    if line.split()[-1].startswith('crop'):
        print line.split()[-1] 


>>> 
crop=400:704:440:8
crop=400:704:440:8
crop=400:704:440:8
crop=400:704:440:8
crop=400:704:440:8
crop=400:704:440:8
crop=400:704:440:8

答案 2 :(得分:0)

您可以使用awk打印第n列:

awk '{print $14}' file

您可以使用以下语法过滤掉所有不包含至少14列的行:

awk 'NF==14 {print $14}' file

答案 3 :(得分:0)

您尝试使用正则表达式吗?我会建议这种模式:

((crop=)(\d|:)*(\d))

您可以使用http://regex101.com/构建自己的新照片。

答案 4 :(得分:0)

如果您将每个行作为单独的字符串,则可以将该行的最后一个子字符串提取为:

# STRING FOR THAT LINE
s = """[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:650650 t:10.844167 crop=400:704:440:8"""

# EXTRACT LAST SUBSTRING
s.split()[-1]

答案 5 :(得分:0)

如果

x = "crop=400:704:440:8"

然后使用

x[-1]

它将返回字符串

中的最后一个字符