2015-01-13 115 views
0

通常我用C写入#从一行中获取子字符串

如何剪切字符串? 我有这样一行:

Line 58: Oct 6 16:58:03 INTEG_245 sia_server[6830]: DbsinkConsumer.cc:262: (D) <video> 07920E: Got msg_idx=28 for evt_id=436752 

,我需要削减28?

这是我使用的代码:

if (str(line).find("msg_idx=") > 0): 
    msg_id = line[line.index("Got"):line.index("For")] 

得到了一个错误:

sg_id = line[line.index("Got"):line.index("For")] 
ValueError: substring not found 

将竭诚为例子

+0

你试过字符切片? –

+1

尝试'line.index(“for”)'(小写'f') –

+0

什么是字符串切片? –

回答

4

您可以使用regular expressions

>>> import re 
>>> s= 'Line 58: Oct 6 16:58:03 INTEG_245 sia_server[6830]: DbsinkConsumer.cc:262: (D) <video> 07920E: Got msg_idx=28 for evt_id=436752' 
>>> print int(re.search(r'msg_idx=(\d+)', s).group(1)) 
28 

...在whi ch re.search()搜索表达式'msg_idx=',其前面是r,表明它是具有转义序列的RE,后面是捕获组(),后面可以引用它。里面的\d+表示至少一个数字字符。然后group(1)指在规定的捕获组在位置1

+0

不错!!!!!!!!!没有想过使用正则表达式! :) –

+0

嗨我试过这个,我得到了错误:self.msg_id2 = re.search(r'(?<= msg_idx =)\ d +',s).group(0)TypeError:期望的字符串或缓冲区 - Barak Rosenfeld 51 secs ago编辑 –

+0

@BarakRosenfeld:我猜你的s变量不是字符串。 –

1

它不使用line.index(example_word)一个很好的方式,也许你有很多的example_word在你txext和索引只返回第一个匹配的索引。您可以使用re.sub和积极look-behind作为一种更有效的方式:

>>> s="Line 58: Oct 6 16:58:03 INTEG_245 sia_server[6830]: DbsinkConsumer.cc:262: (D) <video> 07920E: Got msg_idx=28 for evt_id=436752" 
>>> re.sub(r'(?<=msg_idx=)\d+','',s) 
'Line 58: Oct 6 16:58:03 INTEG_245 sia_server[6830]: DbsinkConsumer.cc:262: (D) <video> 07920E: Got msg_idx= for evt_id=436752' 

,如果你想获得28你可以使用re.search

>>> s="Line 58: Oct 6 16:58:03 INTEG_245 sia_server[6830]: DbsinkConsumer.cc:262: (D) <video> 07920E: Got msg_idx=28 for evt_id=436752" 
>>> re.search(r'(?<=msg_idx=)\d+',s).group(0) 
'28' 
#or just use grouping : 
>>> re.search(r'msg_idx=(\d+)',s).group(1) 
'28' 
+0

嗨我试过这个,我得到了错误:self.msg_id2 = re.search(r'(?<= msg_idx =)\ d +',s).group (0) TypeError:期望的字符串或缓冲区 –

+0

@BarakRosenfeld's'是你的字符串! – Kasramvd

+0

是的谢谢,我发现这个问题非常感谢! :) –

相关问题