python正则表达式匹配可选的方括号 [英] python regex match optional square brackets

查看:535
本文介绍了python正则表达式匹配可选的方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串:

1 "R J BRUCE & OTHERS V B J & W L A EDWARDS And Ors CA CA19/02 27 February 2003",     
2 "H v DIRECTOR OF PROCEEDINGS [2014] NZHC 1031 [16 May 2014]",  
3 '''GREGORY LANCASTER AND JOHN HENRY HUNTER V CULLEN INVESTMENTS LIMITED AND  
ERIC JOHN WATSON CA CA51/03 26 May 2003''' 

我正在尝试找到一个与所有匹配的正则表达式.我不知道如何在字符串末尾的日期周围匹配可选的方括号,例如[2014年5月16日].

I am trying to find a regular expression which matches all of them. I don't know how to match optional square brackets around the date at the end of the string eg [16 May 2014].

casename = re.compile(r'(^[A-Z][A-Za-z\'\(\) ]+\b[v|V]\b[A-Za-z\'\(\) ]+(.*?)[ \[ ]\d+    \w+ \d\d\d\d[\] ])', re.S) 

最后一个日期正则表达式仅匹配带方括号的日期的情况,而不匹配不带日期的情况.

The date regex at the end only matches cases with dates in square bracket but not the ones without.

感谢所有回答的人. @Matt Clarkson我要匹配的是一个更大文本中的司法裁决句柄".这些句柄之间有很大的差异,但是它们都从一行的开头开始,在聚会名称和结尾的日期之间带有"v"代表.多数情况下,当事方的名字都用大写字母表示,但不仅仅用大写字母表示.我正在尝试每个文档只有一个匹配项,并且没有误报.

Thank to everybody who answered. @Matt Clarkson what I am trying to match is a judicial decision 'handle' in a much larger text. There is a large variation within those handles, but they all start at the beginning of a line have 'v' for versus between the party names and a date at the end. Mostly the names of the parties are in capital but not exclusively. I am trying to have only one match per document and no false positives.

推荐答案

我使用此方法将它们全部匹配(您需要添加不区分大小写的标志):

I got all of them to match using this (You'll need to add the case-insensitive flag):

(^[a-z][a-z\'&\(\) ]+\bv\b[a-z&\'\(\) ]+(?:.*?) \[?\d+ \w+ \d{4}\]?)

正则表达式演示

说明:

  • (开始捕获组
    • [a-z\'&\(\) ]+匹配该组中的一个或多个字符
    • \b匹配单词边界
    • v从字面上匹配字符'v'
    • \b匹配单词边界
    • [a-z&\'\(\) ]+匹配该组中的一个或多个字符
    • (?:开始非捕获组
      • .*?匹配任何内容
      • ( Begin capture group
        • [a-z\'&\(\) ]+ Match one or more of the characters in this group
        • \b Match a word boundary
        • v Match the character 'v' literally
        • \b Match a word boundary
        • [a-z&\'\(\) ]+ Match one or more of the characters in this group
        • (?: Begin non-capturing group
          • .*? Match anything

          这篇关于python正则表达式匹配可选的方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆