正则表达式将变量字符串拆分为groupdict

时间:2015-08-28 08:49:38

标签: python regex python-2.7

我想从我的数据中提取一些信息。

最完整的行可能如下(每个部分可能包含CJK字符):

0. (event) (tag) [group (artist)] title (form) [addition1] [addition2]

一行也可能是:

1. (event) [group (artist)] title (form) [addition1]

2. [event] [group (artist)] title (form) (addition1)

3. (tag) [group (artist)] title

4. [group (artist)] title

5. title

6. and something like above, such as 【tag】 [group (artist)] title 【form】

如我们所见,最简单的行只是纯文本title, 我写了一个正则表达式尝试匹配所有这些

import re
regex_patern = ur'([\(\[](?P<event>[^\)\]]*)[\)\]])?\s*([\(\[](?P<type>[^\)\](\)\])]*)[\)\]])?\s*(\[(?P<group>[^\(\]]*)(\((?P<artist>[^\)]*)\))?\])?(?P<title>[^\(\)\[\]]*)([\(\[](?P<from>[^\)\]]*)[\)\]])?(\s*[\(\[](?P<more1>[^\)\]]*)[\)\]])'

p = re.compile(regex_patern)

rows= [
'(event) (tag) [group (artist)] title (form) [addition1] [addition2]',
'(event) [group (artist)] title (form) [addition1]',
'[event] [group (artist)] title (form) (addition1)',
'(tag) [group (artist)] title',
'[group (artist)] title',
'title',
]

for r in rows:
    r = re.search(p, r)
    print r.groupdict()

输出:

{u'from': 'form', u'more1': 'addition1', u'artist': 'artist', u'title': ' title ', u'group': 'group ', u'type': 'tag', u'event': 'event'}
{u'from': 'form', u'more1': 'addition1', u'artist': 'artist', u'title': ' title ', u'group': 'group ', u'type': None, u'event': 'event'}
{u'from': 'form', u'more1': 'addition1', u'artist': 'artist', u'title': ' title ', u'group': 'group ', u'type': None, u'event': 'event'}
{u'from': None, u'more1': 'group (artist', u'artist': None, u'title': '', u'group': None, u'type': None, u'event': 'tag'}
{u'from': None, u'more1': 'group (artist', u'artist': None, u'title': '', u'group': None, u'type': None, u'event': None}
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-831c548bc3f0> in <module>()
     15 for r in rows:
     16     r = re.search(p, r)
---> 17     print r.groupdict()

AttributeError: 'NoneType' object has no attribute 'groupdict'

第4行的结果出乎意料。
我认为re应该从中间搜索。首先查找[group (artist)] and title,但我不知道如何用正则表达式编写。 或者我做错了?

1 个答案:

答案 0 :(得分:1)

修改

看起来(至少在您提供的示例中)您可以正确匹配并将整个字符串分组:

^(?:(?:^[\[()](?P<event>[^)\]]+)[)\]](?=.+[\])]$)\s)?(?:[(【](?P<tag>(?<=^[(【])[^】)]+(?=.+[\w】]$)|(?<=\)\s\()[^)]+(?=\)\s\[))[】)]\s)?\[(?:(?P<group>[^(\]]+)\s+\((?P<artist>[^)]+)\)\])\s+)?(?P<title>[^(\n)【]+)(?:\s*[\(【](?P<form>[^)】]+)[)】](?:\s*[\[(](?P<add>[^\])]+)[\])])?(?:\s*[\[(](?P<add2>[^\])]+)[\])])?)?$

DEMO

用于:

import re

rows= [
'(event) (tag) [group (artist)] title (form) [addition1] [addition2]',
'(event) [group (artist)] title (form) [addition1]',
'[event] [group (artist)] title (form) (addition1)',
'(tag) [group (artist)] title',
'[group (artist)] title',
'title',
]

p = re.compile(ur'^(?:(?:^[\[()](?P<event>[^)\]]+)[)\]](?=.+[\])]$)\s)?(?:[(【](?P<tag>(?<=^[(【])[^】)]+(?=.+[\w】]$)|(?<=\)\s\()[^)]+(?=\)\s\[))[】)]\s)?\[(?:(?P<group>[^(\]]+)\s+\((?P<artist>[^)]+)\)\])\s+)?(?P<title>[^(\n)【]+)(?:\s*[\(【](?P<form>[^)】]+)[)】](?:\s*[\[(](?P<add>[^\])]+)[\])])?(?:\s*[\[(](?P<add2>[^\])]+)[\])])?)?$')

for r in rows:
    [m.groupdict() for m in p.finditer(r)]
    print m.groupdict()

给出输出:

{u'event': 'event', u'tag': 'tag', u'group': 'group', u'artist': 'artist', u'title': 'title ', u'form': 'form', u'add': 'addition1', u'add2': 'addition2'} 
{u'event': 'event', u'tag': None, u'group': 'group', u'artist': 'artist', u'title': 'title ', u'form': 'form', u'add': 'addition1', u'add2': None} 
{u'event': 'event', u'tag': None, u'group': 'group', u'artist': 'artist', u'title': 'title ', u'form': 'form', u'add': 'addition1', u'add2': None} 
{u'event': None, u'tag': 'tag', u'group': 'group', u'artist': 'artist', u'title': 'title', u'form': None, u'add': None, u'add2': None} 
{u'event': None, u'tag': None, u'group': 'group', u'artist': 'artist', u'title': 'title', u'form': None, u'add': None, u'add2': None} 
{u'event': None, u'tag': None, u'group': None, u'artist': None, u'title': 'title', u'form': None, u'add': None, u'add2': None}

DEMO

这个正则表达式由几个部分组成:

  • (?:^[\[()](?P<event>[^)\]]+)[)\]](?=.+[\])]$)\s)? - 匹配事件
  • (?:[(【](?P<tag>(?<=^[(【])[^】)]+(?=.+[\w】]$)|(?<=\)\s\()[^)]+(?=\)\s\[))[】)]\s)? - 匹配代码
  • \[(?:(?P<group>[^(\]]+)\s+\((?P<artist>[^)]+)\)\])\s+)? - 匹配群组
  • (?P<title>[^(\n)【]+) - 匹配标题
  • (?:\s*[\(【](?P<form>[^)】]+)[)】](?:\s*[\[(](?P<add>[^\])]+)[\])])?(?:\s*[\[(](?P<add2>[^\])]+)[\])])?)? - 匹配表单并添加

如您所见,除了与title匹配的部分之外,每个部分都以?量词结束,这意味着零或一个。因此,这些部分是可选的,如果有匹配的片段,它将匹配,但如果没有,它将不会打扰(至少它不应该)正则表达式的其余部分将如何工作。这就是为什么它似乎与“从中间”匹配,而不是“从左到右”。