【问题标题】:Not able to retrieve all the groups using regular expression in Python无法在 Python 中使用正则表达式检索所有组
【发布时间】: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)

【问题讨论】:

标签: python regex python-3.x python-3.5


【解决方案1】:

你可以去:

sections\ 
(?P<section1>[-.\w()]+)
\ and\ 
(?P<section2>[-.\w()]+)
(?P<optional>
    (?:,\ [-.\w()]+)+
)?

然后使用re.finditer(),参见a demo on regex101.com
这会在“可选”组中捕获逗号分隔的组,您需要以编程方式将这些组用逗号分隔。

import re
rx = re.compile("""
sections\ 
(?P<section1>[-.\w()]+)
\ and\ 
(?P<section2>[-.\w()]+)
(?P<optional>
    (?:,\ [-.\w()]+)+
)?""", re.VERBOSE)

sections = [(m.group('section1'), m.group('section2')) for m in rx.finditer(your_text_here)]
print sections
# [('1.1441-1(e)(4)(iv)(C)', '1.1471-3(c)(6)(iv)'), ('1.1441-1(e)(12)(i)(23)', '1.11-3(3)(4)(i)')]

完整的demo can be found on ideone.com

【讨论】:

  • 感谢您的回答,但如果您有两个以上的值,其中包含 1.44-3(a)(g) 和 2.34-6(k)(6) 和 6.39-5(0) 部分,该怎么办(g) 等等?
  • @Praveen:您的示例没有这些案例,请编辑您的问题并提供一些。
  • 问题已更新。根据我的正则表达式,您可以看到有重复捕获,我只能获得最后一次捕获。那么我们该如何解决这个特殊问题呢?
  • @Praveen:查看更新的答案。使用这种重复模式,您只能将可选组捕获为一个整体,而不是单个部分。
【解决方案2】:

删除(?i) 并且正则表达式适用于您的示例...您还使用非捕获组,并且正则表达式不会为您提供单独的组来重复相同的捕获组,它只会保留最后一个,这就是为什么您会看到它输出为同一事物的两个副本。

就我个人而言,我会抓住整场比赛,然后执行以下操作:

result = []
for match in matches:
    pieces = match.split(' ')
    result.append((pieces[1], pieces[3]))
return result

给了

('1.1441-1(e)(12)(i)(23)', '1.11-3(3)(4)(i)') 每场比赛。

【讨论】:

    猜你喜欢
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多