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

秘银

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

最完整的行可能如下所示(每个部分可能包含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,但是我不知道如何用正则表达式编写。还是我做错了路?

塞基耶拉

编辑

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

^(?:(?:^[\[()](?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>[^\])]+)[\])])?)?$

演示

用于:

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}

演示

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

  • (?:^[\[()](?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>[^\])]+)[\])])?)? -匹配表格并添加

如您所见,每个部分(不包括与a匹配的部分)都title?量词结尾,即零或一。因此,这些部分是可选的,如果有要匹配的片段,它将匹配,但是如果没有,它将不会打扰(至少不应该)正则表达式的其余部分如何工作。这就是为什么它看起来像“从中间”匹配,而不是“从左到右”匹配的原因。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

正则表达式将长表达式字符串拆分为单个表达式的数组

使用正则表达式将字符串拆分为句子

使用sql中的正则表达式将字符串拆分为多列

如何使用正则表达式将字符串拆分为几个部分

Python正则表达式:拆分为空字符串的模式匹配

Java:使用正则表达式将字符串拆分为char数组

使用正则表达式将字符串拆分为3个部分

Javascript,正则表达式将字符串拆分为括号之间的多维数组

正则表达式,将字符串拆分为数组 vb.net

正则表达式将字符串拆分为 args 而不破坏引用的文本

通过正则表达式将字符串拆分为列表

如何使用给定的正则表达式将字符串拆分为数组

将 ruby 字符串拆分为多个散列的正则表达式

正则表达式-根据标点/空格将字符串拆分为数组

PHP Codeigniter通过正则表达式将字符串拆分为数组

通过正则表达式匹配将字符串拆分为单独的列表

无法了解正则表达式拆分为字符串的结果

正则表达式将字符串拆分为非常大的块

Javascript将字符串拆分为数组字典(键->值)(正则表达式)

正则表达式将数据帧字符串拆分为python中的列

使用正则表达式将字符串拆分为组?

Python-通过正则表达式列表将字符串拆分为列表

Java正则表达式将字符串拆分为不同的变量

使用php中的正则表达式将字符串拆分为数组

在可变模式上使用正则表达式拆分字符串

如何使用正则表达式从多行字符串获取groupdict

正则表达式将字符串拆分为浮点数/数字和字符串

使用正则表达式将字符串拆分为多个字符串

使用正则表达式在Android中将字符串拆分为多个字符串