更新regex以搜索多个字符串

2024-09-26 22:07:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面一行我正在搜索单词“Bluetooth”,现在我想搜索多个字符串,即Bluetooth,MAP,FTP..如何更新regex来做到这一点?你知道吗

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)

Tags: to字符串remaplineftp单词process
3条回答

在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

不过,我不确定这是你想要的,因为这符合最新的蓝牙以及(因为忽略案例). 如果您需要区分大小写的搜索,那么这应该可以:

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

希望对你有所帮助。你知道吗

干杯

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

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

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

我不明白你到底想做什么。你能解释一下吗?你知道吗

如果你想测试你的模式(蓝牙,地图,FTP…)是否存在于一个多字符串中,你可以使用这个检索,当它找到匹配项时,返回值将是MatchObject,否则将为None

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将为每个yeild返回一个MatchObject。你知道吗

下面是基于您的请求的此方法的三个测试代码

>>> 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')

相关问题 更多 >

    热门问题