使用PDFminer(python)在PDF中查找正则表达式不起作用 [英] Finding regex in PDF with PDFminer (python) not working
本文介绍了使用PDFminer(python)在PDF中查找正则表达式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在一个简短的pdf中查找一个正则表达式.但是,它不起作用.我不明白为什么,因为如果我尝试搜索一个简单的字符串,我不会遇到任何问题.文本正确呈现.这是我的代码:
I'm trying to find occurrences of a regular expression in a short pdf. However, it doesn't work. I don't understand why, because if I try to search a simple string I don't have problems. The text is rendered correctly. Here is my code:
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from io import StringIO
import re
def convert_pdf_to_txt(path):
#\[\s*prima(?!\S)regex = re.compile(r"\[(\s)prima(?!\S)")
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = open(path, 'rb')
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0
caching = True
pagenos=set()
for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
interpreter.process_page(page)
text = retstr.getvalue()
fp.close()
device.close()
retstr.close()
reg = re.compile(r"\[(\s)prima(?!\S)")
matches = re.findall(reg, text)
return matches
print(convert_pdf_to_txt("fel_split.pdf"))
这是我的正则表达式:(r"\[(\s)prima(?!\S)")
我想找到"[prima".
This is my regex: (r"\[(\s)prima(?!\S)")
I want to find "[ prima ".
推荐答案
也许有多个空格字符?试试这个:r"\]\s*prima\b"
Maybe there are more than one space char? Try this: r"\]\s*prima\b"
UPD:,并且在示例文档中出现时,应该使用另一个括号:]
,而不是[
.
UPD: and as it appears from the sample document another bracket should've been used: ]
instead of [
.
这篇关于使用PDFminer(python)在PDF中查找正则表达式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文