python - 在 Python 中使用 AND 条件查找两个模式之间的字符串
问题描述
我想识别两个模式之间的字符串(lettre/
例如,“和”)。此外,识别的字符串不应该对应于第三个模式(somth?other
例如)。
在 MAC OSX 10.13 上运行的 Python 3.7
import re
strings = ['lettre/abc"','lettre/somth?other"','lettre/acc"','lettre/edf"de','lettre/nhy"','lettre/somth?other"']
res0_1 = re.search('lettre/.*?\"', strings[0])
res1_1 = re.search('lettre/.*?\"', strings[1])
print(res0_1)
<re.Match object; span=(0, 11), match='lettre/abc"'>
print(res1_1)
<re.Match object; span=(0, 19), match='lettre/somth?other"'>
res0_2 = re.search('lettre/(.*?\"&^[somth\?other])', strings[0])
res1_2 = re.search('lettre/(.*?\"&^[somth\?other])', strings[1])
print(res0_2)
None
print(res1_2)
None
我想得到res0_1
forstrings[0]
和res1_2
for strings[1]
。
解决方案
据我了解试试这个:
import re
strings = ['lettre/abc"','lettre/somth?other"','lettre/acc"','lettre/edf"de','lettre/nhy"','lettre/somth?other"']
res0_1 = re.findall('lettre/(.*)\"', strings[0])
res1_2 = re.findall('lettre/(.*)\"', strings[1])
print(res0_1)
print(res1_2)
希望能帮助到你
推荐阅读
- git - 有没有办法用 git 查看树的 base_tree?
- linux - Gapcloser内存故障
- css - 反应字体真棒图标未显示在css内容中
- java - 在 php Android Studio 中检查重复项时出错
- rust - 如何调试不安全块中的非法硬件指令错误?
- c# - Asp.net 自定义剃须刀视图引擎指定 cshtml 文件位置
- javascript - 通过 Ajax 将图像传递给 php
- android - Android:从 RecyclerView 打开 PDF
- graphql - 如何使用 Axios 和 GraphQL 发布对象数据
- python-3.x - 如何执行函数并保存在新的 csv 文件中?