我有下面一行我正在搜索单词“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)
在re模块中,您可以使用findall方法,该方法将返回匹配字符串的列表
不过,我不确定这是你想要的,因为这符合最新的蓝牙以及(因为忽略案例). 如果您需要区分大小写的搜索,那么这应该可以:
希望对你有所帮助。你知道吗
干杯
m = re.search('\n(Bluetooth:|MAP|FTP) (.*)\n', line, re.IGNORECASE)
有效吗?你知道吗编辑:我没有注意到上面的整个示例字符串。现在我开始认为您的意思是要匹配一个以“Bluetooth:”开头,然后包含“MAP”或“FTP”的字符串?你知道吗
如果是这样的话:
\nBluetooth:(.*)(MAP|FTP)(.*)\n
我不明白你到底想做什么。你能解释一下吗?你知道吗
如果你想测试你的模式(蓝牙,地图,FTP…)是否存在于一个多字符串中,你可以使用这个检索,当它找到匹配项时,返回值将是MatchObject,否则将为None
如果你想找到所有的线匹配你的模式,你可以使用
或者
区别在于findall将返回一个元组列表,finditer将为每个yeild返回一个MatchObject。你知道吗
下面是基于您的请求的此方法的三个测试代码
相关问题 更多 >
编程相关推荐