更新正则表达式以搜索多个字符串

时间:2013-10-23 00:38:30

标签: python

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

3 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分: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

但是我不确定这是你想要的,因为这也与laster蓝牙相匹配(因为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

希望有所帮助。

干杯,

答案 2 :(得分: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')