2013-10-23 93 views
0

我有以下線路,我正在搜索單詞「藍牙」,現在我想搜索多個字符串,即藍牙,地圖,FTP ..如何更新正則表達式來做這個?更新正則表達式來搜索多個字符串

line = 'Bluetooth: Merging two BT process into one BT process to reduce memory. This patch enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context.' 
m = re.search('\nBluetooth: (.*)\n', line, re.IGNORECASE) 

回答

1

does m = re.search('\n(Bluetooth:|MAP|FTP) (.*)\n', line, re.IGNORECASE) work?

編輯:我沒有注意到上面的整個示例字符串。現在我開始認爲你的意思是你想匹配一個以「Bluetooth:」開頭並且包含'MAP'或'FTP'的字符串?

,如果是這樣的話是這樣的: \nBluetooth:(.*)(MAP|FTP)(.*)\n

我不明白究竟你試圖完成。你能解釋一下嗎?

可以使用的findAll方法將返回匹配的字符串

import re 
line = 'Bluetooth: Merging two BT process into one BT process to reduce memory. This patch  enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context.' 
m = re.findall('bluetooth|map|ftp', line, re.IGNORECASE) 
print m 

列表re模塊

+0

從例句它看起來像結腸可能需要被移動到像'\ n中的括號(藍牙:| MAP | FTP)',但否則這對我來說看起來不錯。 –

0

但是我不知道這是你想要的,因爲這將拉斯特藍牙匹配,以及(因爲的re.ignorecase)。如果你想區分大小寫的搜索,那麼這應該工作:

import re 
line = 'Bluetooth: Merging two BT process into one BT process to reduce memory. This patch  enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context.' 
m = re.findall('Bluetooth|MAP|FTP', line) 
print m 

希望幫助一些。

乾杯,

0

如果你想測試你的模式(藍牙,MAP,FTP ..)是否不存在於多個字符串,你可以使用這個re.search,返回值將是一個MatchObject當它找到一個匹配,否則將無

re.search('^(Bluetooth|MAP|FTP):.+$', s, re.IGNORECASE|re.M) 

,如果你想找到的所有行匹配你的模式,你可以使用

ret = re.findall('^((Bluetooth|MAP|FTP):.+$)', s, re.IGNORECASE|re.M) 

re.finditer('^((Bluetooth|MAP|FTP):.+$)', s, re.IGNORECASE|re.M) 

不同之處在於findall會返回一個元組列表,而finditer會返回一個MatchObject foreach yeild。

下面是三個這種方法基礎的測試代碼對你的要求

>>> s = '\nBluetooth: Merging two BT process into one BT process to reduce memory. This patch enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context.\nBluetooth: Merging two BT process into one BT process to reduce memory. This patch enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context.\nBluetooth: Merging two BT process into one BT process to reduce memory. This patch enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context.\n\nMerging two BT process into one BT process to reduce memory. This patch enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context.\n' 
>>> ret = re.search('^(Bluetooth|MAP|FTP):.+$', s, re.IGNORECASE|re.M) 
>>> ret.groups() 
('Bluetooth',) 
>>> ret = re.findall('^((Bluetooth|MAP|FTP):.+$)', s, re.IGNORECASE|re.M) 
>>> ret 
[('Bluetooth: Merging two BT process into one BT process to reduce memory. This patch enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context.', 'Bluetooth'), ('Bluetooth: Merging two BT process into one BT process to reduce memory. This patch enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context.', 'Bluetooth'), ('Bluetooth: Merging two BT process into one BT process to reduce memory. This patch enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context.', 'Bluetooth')] 
>>> for m in re.finditer('^((Bluetooth|MAP|FTP):.+$)', s, re.IGNORECASE|re.M): 
...  if m: 
...   print(m.group()) 
...   
Bluetooth: Merging two BT process into one BT process to reduce memory. This patch enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context. 
Bluetooth: Merging two BT process into one BT process to reduce memory. This patch enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context. 
Bluetooth: Merging two BT process into one BT process to reduce memory. This patch enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context. 
>>> for m in re.finditer('^((Bluetooth|MAP|FTP):.+$)', s, re.IGNORECASE|re.M): 
...  if m: 
...   print(m.groups()) 
...   
('Bluetooth: Merging two BT process into one BT process to reduce memory. This patch enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context.', 'Bluetooth') 
('Bluetooth: Merging two BT process into one BT process to reduce memory. This patch enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context.', 'Bluetooth') 
('Bluetooth: Merging two BT process into one BT process to reduce memory. This patch enables BTC , MAP , FTP & SAP modules to run in com.android.bluetooth context.', 'Bluetooth') 
相關問題