python - 如何在 python docx 中使用正则表达式?
问题描述
我想在 docx 文档中找到特定的正则表达式。我安装了python-docx
,我可以在我的文本中找到字符串。但是,我想使用正则表达式。
到目前为止,我的代码是:
import re
from docx import Document
doc = Document('categoriemanzoni.docx')
match = re.search(r"\[(['prima']+(?!\S))", doc)
for paragraph in doc.paragraphs:
paragraph_text = paragraph.text
if match in paragraph.text:
print('ok')
对我来说,它似乎也没有阅读所有段落。如何解决?
解决方案
您的代码在错误的位置应用了正则表达式(它本身是错误的)。你可能想要这样的东西:
import re
from docx import Document
doc = Document('categoriemanzoni.docx')
regex = re.compile(r"\[prima(?!\S)")
for paragraph in doc.paragraphs:
if regex.search(paragraph.text):
print('ok')
推荐阅读
- css - go buffalo 工作流中的 /public/assets 在哪里生成?
- reactjs - 未找到反应路线
- sql - 从计数子查询的结果更新列
- angular - 触摸角形复选框
- angular - 添加计时器后,AbstractControl 不包含异步验证错误
- vb.net - 检查对象是否存在于 Visual Basic 中的列表索引处
- java - 使用 compareTo 对一个数组进行升序排序
- if-statement - 如何使用控制结构在 LIPS 上使用 mod 功能
- c# - WPF 特定的 TabItem 宽度行为
- c# - VS Studio / IIS Express 阻止 js 和 css - 但仅在本地驱动器上