2015-01-13 112 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

是的謝謝,我發現這個問題非常感謝! :) –

相關問題