python - 如何使用正则表达式从迭代器中获得完全匹配?
问题描述
我已经使用 + 和 $ 来标记单词的开头和结尾,但我不知道如何将其放入编译中而不会出现语法错误。不断发生的是匹配也会找到更大的单词,我只想要完全匹配。
>>> mumu = ["act", "magic", "lose", "dance"]
>>> matcher = ["That is a terrible actor", "This is the first act", "It works like magic", "That's because he is a magician", "Don't be afraid to lose", "Don't be a loser", "Try this dance", "It won't make you a dancer"]
>
> >>> def intermediate(first, second):
... dul = len(second)
... milly = cycle(second)
... for y in range(dul):
... t = re.compile(next(milly))
... for i in first:
... print(t)
... if re.search(t, i) != None:
... print(i)
... dul -= 1
... continue
... else:
... print("try again")
... dul -= 1
... continue
... continue
结果是
re.compile('act')
That is a terrible actor
re.compile('act')
This is the first act
解决方案
推荐阅读
- node.js - 从 URL 下载多个图像并从 aws lambda (nodejs) 上传到 aws s3
- cpu-architecture - 为什么 LEGv8 中 d 格式的 9 位地址意味着寄存器可以加载基址寄存器 Rn 中地址的 2^8 字节的双字?
- html - 如何将页脚推到页面底部
- r - 从lmList绘制β系数?
- python - 如何获取 Django 模型的多对多字段总数?
- android - 双卡铃声 Uri
- python - 将 3d 张量输入 CNN 和 keras 网络时出现尺寸错误
- android - React-Native 构建 apk 组件不在设备上运行
- c# - 是否可以为非 UI 工作线程创建 Dispatcher (System.Windows.Threading)?
- rest - 在 Flask REST API 代码中生成 SQLite 数据库