【问题标题】:RegExp to split string according to different criteria in pythonRegExp根据python中的不同标准拆分字符串
【发布时间】:2015-02-03 13:39:22
【问题描述】:

我想用正则表达式分割字符串。

例如

when [python] or [html ] demo  "css html"   -[javascript] score:5

从我想要的这个字符串,以下列表,

contains = ['when', 'demo']
word_press = ["css html"]
tags = ['python', 'or', 'html', '-', 'javascript']
options = [{score:5}]
  • "[]"(括号)中的所有单词都是标签列表。
  • "" 之间的单词将在 word_press 列表中。
  • 单词中包含: 的单词将出现在选项列表中。
  • 除上述条件之外的其他条件将在包含列表中。

我试过了,

((?:or\s|-)?\[.*?\])|(".*?")|([a-z]+:\d*)|(\S+)

live demo

它工作正常,但我在 python 中使用它

>>> import re
>>> s = '''[python] or [html] how to "how to" user:2525
... [demo] how to createscore:5
... when [python] or [html] demo  "css html"   -[javascript] score:5'''
>>> re.findall('''((?:or\s|-)?\[.*?\])|(".*?")|([a-z]+:\d*)|(\S+)''', s)
[('[python]', '', '', ''),
 ('or [html]', '', '', ''),
 ('', '', '', 'how'),
 ('', '', '', 'to'),
 ('', '"how to"', '', ''),
 ('', '', 'user:2525', ''),
 ('[demo]', '', '', ''),
 ('', '', '', 'how'),
 ('', '', '', 'to'),
 ('', '', 'createscore:5', ''),
 ('', '', '', 'when'),
 ('[python]', '', '', ''),
 ('or [html]', '', '', ''),
 ('', '', '', 'demo'),
 ('', '"css html"', '', ''),
 ('-[javascript]', '', '', ''),
 ('', '', 'score:5', '')]

它返回列表中的元组。有没有办法获取像

这样的组
group1 = ['[python]', 'or [html]', '[demo]', '[python]', 'or [html]', '-[javascript]']
...

【问题讨论】:

  • 你在什么基础上尝试像上面那样拆分?
  • 为什么or 是一个“标签”?
  • split string according to different criteria using python,你能解释一下标准吗?
  • 我不会使用单个正则表达式,而是使用扫描仪。 re 模块中有一个undocumented Scanner 类,它使得编写这样的扫描器变得非常简单。
  • 您在寻找类似this 的东西吗?如果您更清楚自己的标准会很有帮助。

标签: python regex


【解决方案1】:
>>> import re
>>> s = '''[python] or [html] how to "how to" user:2525
[demo] how to createscore:5
when [python] or [html] demo  "css html"   -[javascript] score:5'''

这是一种可能的正则表达式(包括内联 cmets),用于捕获您需要的信息(参见演示 here):

>>> pattern = r'''
    (?P<tag>                 # define group one - tags
    (?:or\s|-)?              # - acceptable words/chars for preceding tags
    \[.*?\])                 # - tag definition - words in square brackets
    |(?P<word_press>".*?")   # group two - words in quotes
    |(?P<options>[a-z]+:\d*) # group three - options with colons
    |(?P<other>\S+)          # group four - anything left over
'''

请注意,将其与 findall 一起使用将为您提供元组列表:

>>> re.findall(pattern, s, re.VERBOSE)
[('[python]', '', '', ''),
 ('or [html]', '', '', ''),
 ('', '', '', 'how'), 
 ('', '', '', 'to'),
 ('', '"how to"', '', ''),
 ('', '', 'user:2525', ''), 
 ('[demo]', '', '', ''),
 ('', '', '', 'how'),
 ('', '', '', 'to'), 
 ('', '', 'createscore:5', ''),
 ('', '', '', 'when'),
 ('[python]', '', '', ''), 
 ('or [html]', '', '', ''), 
 ('', '', '', 'demo'), 
 ('', '"css html"', '', ''), 
 ('-[javascript]', '', '', ''), 
 ('', '', 'score:5', '')]

但这是一种重新排列它的函数式编程方式:

>>> from functools import partial
>>> map(partial(filter, None), zip(*re.findall(pattern, s, re.VERBOSE)))
[('[python]', 'or [html]', '[demo]', '[python]', 'or [html]', '-[javascript]'), 
 ('"how to"', '"css html"'), 
 ('user:2525', 'createscore:5', 'score:5'), 
 ('how', 'to', 'how', 'to', 'when', 'demo')]

【讨论】:

  • 可以使用组读取tule ??像match.group('tag') 它返回标签列表??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-06
  • 2020-09-25
  • 1970-01-01
  • 2016-11-01
  • 2018-05-12
  • 2018-05-27
相关资源
最近更新 更多