【发布时间】:2016-11-03 03:31:36
【问题描述】:
我对 Python 非常陌生,并且编写了正则表达式,我能够匹配完整的模式,但无法检索所有捕获的匹配项,如下所示。有人可以帮我解决以下内容
正则表达式
(?i)(?:sections?|\&\#xA7\;|Treas\.\s*Reg\.)\s*((\d+\.\d+\-\d+(?:\((?:[a-zA-Z]|[0-9]+|[i v x]+)\))*)(?:\s*(?:and|\,|\,\s*and)\s*)*)+")
输入要匹配的内容:
1. sections 1.1441-1(e)(4)(iv)(C) and 1.1471-3(c)(6)(iv), 1.576-4(a)(9) and 1.32-12(h)(l)
2. sections 1.1441-1(e)(12)(i)(23) and 1.11-3(3)(4)(i) , 1.67-9(k)(10) and 1.78-8
输入:
Q11。有已由收款人填写并签署的 W--8 表格,已扫描 转换成图像或可移植文档格式 (PDF),并上传到第三方 扣缴义务人以电子方式扫描和接收存储库 第 1.1441-1(e)(4)(iv)(C) 和 1.1471-3(c)(6)(iv)、1.576-4(a)(9) 和 1.32-12(h) 节的目的)(l) 如果收款人,在 扣缴义务人要求 W--8 表格记录其第 1.1441-1(e)(12)(i)(23) 和 1.11-3(3)(4)(i) 节,1.67- 9(k)(10) 和 1.78-8 状态 第 3 章和第 4 章的目的,向扣缴义务人发送一封带有链接的电子邮件 到允许扣缴义务人的第三方存储站点 下载存储在存储库中的表格的图像或 PDF 该目的(或收款人以其他方式授权扣缴义务人 以类似的方式从第三方存储库访问特定表单)。
我的输出
['1.32-12(h)(l)','1.78-8']
期望的输出:
['1.1441-1(e)(4)(iv)(C)', '1.1471-3(c)(6)(iv)', '1.576-4(a)(9)', '1.32-12(h)(l)', '1.1441-1(e)(12)(i)(23)', '1.11-3(3)(4)(i)', '1.67-9(k)(10)', '1.78-8']
代码:
import re
class Regex:
def __init__(self,inputtext,regex):
self.regex=regex
self.inputtext=inputtext
def blowCiteQuery(self,inputtext,regex):
mo=regex.findall(inputtext)
print(mo)
if __name__ == "__main__":
text="""Q11. Has a Form W--8 that has been completed and signed by a payee, scanned
into an image or portable document format (PDF), and uploaded to a third--party
repository been scanned and received electronically by a withholding agent for
purposes of sections 1.1441-1(e)(4)(iv)(C) and 1.1471-3(c)(6)(iv), 1.576-4(a)(9) and 1.32-12(h)(l) if the payee, upon
request from the withholding agent for a Form W--8 to document its sections 1.1441-1(e)(12)(i)(23) and 1.11-3(3)(4)(i) , 1.67-9(k)(10) and 1.78-8 status for
purposes of chapters 3 and 4, sends the withholding agent an email with a link
to the third--party repository site that allows the withholding agent to
download the image or PDF of the form that is stored on the repository for
such purpose (or the payee otherwise authorizes the withholding agent to
access the specific form from the third--party repository in a similar manner)."""
regex=re.compile("(?i)(?:sections?|\&\#xA7\;|Treas\.\s*Reg\.)\s*((\d+\.\d+\-\d+(?:\((?:[a-zA-Z]|[0-9]+|[i v x]+)\))*)(?:\s*(?:and|\,|\,\s*and)\s*)*)+")
treglinkval=Regex(text,regex)
treglinkval.blowCiteQuery(text,regex)
【问题讨论】:
-
喜欢this ?
标签: python regex python-3.x python-3.5