如何用正则表达式匹配PDF中的编号语句?
提取PDF中编号完整语句的正则解决方案
我有一份PDF文件,使用PyPDF2提取文本后,需要编写正则表达式捕获其中的编号语句。尝试了两种正则均未达到预期:要么无法捕获完整句子,要么将换行符视为语句分隔符导致内容被拆分为多行。期望将每个编号点捕获为完整的单个句子,示例输出如下:
[ '1. Please admit that Plaintiff, JOSHUA PINK, received benefits from a collateral source, as defined by §768.76, Florida Statutes, for medical bills alleged to have been incurred as a result of the incident described in the Complaint.', '2. Please admit that Plaintiff, JOSHUA PINK, received benefits from a collateral source, as defined by §768.76, Florida Statutes, for loss of wages or income alleged to have been sustained as a result of the incident described in the Complaint.', ... ]
提取到的原始文本
" IN THE CIRCUIT COURT, OF THE EIGHTEENTH JUDICIAL CIRCUIT, IN AND FOR SEMINOLE COUNTY, FLORIDA CASE NO: 2022 -CA-002235 JOSHUA PINK, Plaintiff, vs. MATHEW ZUMBRUM , Defendant. / DEFENDANT'S REQUEST FOR ADMISSIONS TO PLAINTIFF, JOSHUA PINK COME NOW the Defendant , MATHEW ZUMBRUM , by and through the undersigned attorneys, and pursuant to Rule 1.370, Florida Rul es of Civil Procedure, requests the Plaintiff, JOSHUA PINK, admit in this action that each of the following statements are true: 1. Please admit that Plaintiff, JOSHUA PINK , received benefits from a collateral source, as defined by §768.76, Florida Statute s, for medical bills alleged to have been incurred as a result of the incident described in the Complaint. 2. Please admit that Plaintiff, JOSHUA PINK , received benefits from a collateral source, as defined by §768.76, Florida Statutes, for loss of wages o r income alleged to have been sustained as a result of the incident described in the Complaint. 3. Please admit that Plaintiff, JOSHUA PINK , received benefits under the Personal Injury Protection portion of an automobile policy for medical bills alleged to have been incurred as a result of the incident described in the Complaint. Filing # 162442429 E-Filed 12/06/2022 09:46:49 AM 2 4. Please admit that Plaintiff, JOSHUA PINK , received benefits under the Personal Injury Protection portion of an automobile insurance policy for loss of wages or income alleged to have been sustained as a result of the incident described in the Complaint. 5. Please admit that Plaintiff, JOSHUA PINK , received benefits under the medical payments provisions of an automobile insurance policy for medical bills alleged to have been incurred as a result of the incident described in the Complaint. 6. Please admit that Plaintiff, JOSHUA PINK , is subject to a deductible under the Personal Injury Protection portion of an automobile insurance policy. 7. Please admit that Plaintiff, JOSHUA PINK received benefits pursuant to personal or group health insurance policy, for medical bills alleged to have been incurred as a result of the incident described in the Complaint. 8. Please admit that Plaintiff, JOSHUA PINK , received benefits pursuant to a personal or group wage continuation plan or policy, for loss of wages or income alleged to have been sustained as a result of the incident described in the Complaint. 9. Please admit that on the date of the accident alleged in your Complaint, Defendant, MATHEW ZUMBRUM , complied with and met the security requirements under Chapter 627.730 - 627.7405, Florida Statutes. 10. Please admit that Plaintiff, JOSHUA PINK , was partially responsible for the subject accident. 11. Please admit that Plaintiff, JOSHUA PINK , did NOT suffer a permanent injury as a result of the subject accident. I HEREBY CERTIFY that on the 6th day of December, 2022 a true and correct copy of the foregoing was electronically filed with the Florida Court s E-Filing Portal system which will 3 send a notice of electronic filing to Michael R. Vaughn, Esq., Morgan & Morgan, P.A., 20 N. Orange Ave, 16th Floor, Orlando, FL 32801 at mvaughn@forthepeople.com; jburnham@forthepeople.com; mserrano@forthepeople.com. AND REW J. GORMAN & ASSOCIATES BY: (Original signed electronically by Attorney.) LOURDES CALVO -PAQUETTE, ESQ. Attorney for Defendant, Zumbrum 390 N. Orange Avenue, Suite 1700 Orlando, FL 32801 Telephone: (407) 872 -2498 Facsímile: (855) 369 -8989 Florida Bar No. 0817295 E-mail for service (FL R. Jud. Admin. 2.516) : flor.law -mlslaw.172o19@statefarm.com Attorneys and Staff of Andrew J. Gorman & Associates are Employees of the Law Department of State Farm Mutual Automobile Insurance Company. "
现有正则的错误输出
- regex2输出(语句被拆分为多行):
[('2022', 'CA-002235 '), ('1', 'Florida Rul es of Civil Procedure, requests the Plaintiff,'), ('1', 'Please admit that Plaintiff, JOSHUA PINK , received benefits from a collateral'), ('768', 'Florida Statute s, for medical bills alleged to have been incurred as'),...]
- regex1输出(未捕获完整语句):
['1. Please admit that Plaintiff, JOSHUA PINK , received benefits from a collateral ', '2. Please admit that Plaintiff, JOSHUA PINK , received benefits from a collateral ', '3. Please admit that Plaintiff, JOSHUA PINK , received benefits under the Personal ', '2 4. Please admit that Plaintiff, JOSHUA PINK , received benefits under the Personal ', '5. Please admit that Plaintiff, JOSHUA PINK , received benefits under the medical ',....]
原有代码
def read_pdf(name): reader = PdfReader(name,"rb") text = "" for page in reader.pages: text += page.extract_text() + "\n" #regex1 = r'(^[0-9].*)' regex2 = r'([\d]+).+?([a-zA-Z].+).' pat = re.compile(regex, re.M) extracted_text = pat.findall(text) return text,extracted_text text,pdf1 = read_pdf(names[0])
修正后的解决方案
关键改进点
- 文本预处理:修复PDF提取时的断词(如
Rul es→Rules),合并换行内容,去除页码干扰(如2 4.→4.) - 正则优化:使用正向预查匹配完整语句范围,同时启用
re.DOTALL让.匹配换行符
修正代码
import re from PyPDF2 import PdfReader def read_pdf(name): reader = PdfReader(name) text = "" for page in reader.pages: text += page.extract_text() + "\n" # 预处理:合并换行与空格,修复断词,移除页码干扰 processed_text = re.sub(r'\n\s+', ' ', text) processed_text = re.sub(r'(\w+)\s+(\w+)', r'\1\2', processed_text) processed_text = re.sub(r'\d+\s+(\d+\.)', r'\1', processed_text) # 正则匹配:捕获从数字.开始,到下一个数字.或文档结尾的完整语句 regex = r'(\d+\. .*?)(?=\s*\d+\.|$)' pat = re.compile(regex, re.DOTALL) # 去除结果中的多余空格 extracted_text = [item.strip() for item in pat.findall(processed_text)] return text, extracted_text # 调用示例 text, pdf1 = read_pdf("your_pdf_file.pdf") for item in pdf1: print(item)
正则说明
\d+\. .*?:匹配以数字加句号开头的内容,非贪婪模式避免过度匹配(?=\s*\d+\.|$):正向预查,匹配到下一个数字句号或文档结尾,确保捕获完整语句re.DOTALL:让.匹配包括换行在内的所有字符,处理跨多行的语句
内容的提问来源于stack exchange,提问作者Muhamed Khaled
相关产品推荐
相关产品推荐

