2016-11-10 15 views
-1

我想在任何位置用等號拆分字符串。在任何位置使用python拆分帶有等號的字符串

[In] This is a example string abc=xyz this is a example string

有人可以告訴我如何打印後等號簽署的所有字符串(「=」)。例如,在上述情況下,輸出應爲

[Out] abc=xyz

我得到了一些線索,如何分割,如果如果在最後一個字而不是(「=」)裏面字符串的任何地方。

+2

@EdChum這不是輸出OP希望。 OP的輸出和問題並不相同 – MooingRawr

+0

你說你想分割一個字符串,但是你的示例輸入/輸出卻顯示定位了一個帶有「=」的字符串。我正在投票結束這個問題。請澄清你想要的,告訴我們你已經編碼了什麼,以及出了什麼問題。另請參閱[問] –

回答

6

的findall找到所有的occurances,(我認爲這是你的要求)

import re 
a = "[In] This is a example string abc=xyz this is a example string" 
print(re.findall("\w+=\w+",a)) 

OP

['abc=xyz']

+0

或'\ S * = \ S *'.... \ –

+0

@AvinashRaj是的,取決於他的要求 –

+1

'[word for word in'這是一個字符串示例abc = xyz this是一個例子string'.split()if'='in word]'會做同樣的事情,不需要正則表達式。 –

0
s='This is a example string abc=xyz this is a example string' 
l=s.split('=') 

print(l) 

輸出:

['This is a example string abc', 'xyz this is a example string'] 

所以,你要l[-1]獲得的最後一個項目或l[1]獲得的第二項

編輯:如果您要查找的「字」,有一個=其中

s='This is a example string abc=xyz this is a example string' 
l=s.split() 
l = [word for word in l if '=' in word] 
print(l) 

輸出:

['abc=xyz'] 
+2

這不是OP想要的輸出內容。他的問題和輸出不符合 – MooingRawr