0

我有以下行,我正在搜索“蓝牙”这个词,现在我想搜索多个字符串,即蓝牙、MAP、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)
4

3 回答 3

1

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

编辑:我没有注意到上面的整个示例字符串。现在我开始认为您的意思是要匹配以“蓝牙:”开头然后包含“MAP”或“FTP”的字符串?

如果是这样的话: \nBluetooth:(.*)(MAP|FTP)(.*)\n

我不明白你到底想要完成什么。你能解释一下吗?

于 2013-10-23T00:44:09.207 回答
0

If you want to test a your pattern (Bluetooth,MAP,FTP..) whether or not exist in a multiple string you could use this re.search, the return value will be a MatchObject when it find a match otherwise will be None

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

if you want to find all the lines match your pattern you could use

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

or

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

The difference is findall will return a list of tuple and the finditer will return a MatchObject foreach yeild.

Here is the test code for three of this method base on your request

>>> 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')
于 2013-10-23T01:08:12.077 回答
0

在 re 模块中,您可以使用 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.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

希望对一些人有所帮助。

干杯,

于 2013-10-23T00:50:17.037 回答