che*_*run 2 python regex split
这应该是使用re库的一个非常简单的任务.不过,我似乎无法到我的字符串的分隔符分裂]和[.
我已经读过在Python中拆分带有多个分隔符的字符串,Python:带有多个分隔符的拆分字符串,以及Python:如何在方括号内获取多个元素.
我的字符串:
data = "This is a string spanning over multiple lines.
At somepoint there will be square brackets.
[like this]
And then maybe some more text.
[And another text in square brackets]"
Run Code Online (Sandbox Code Playgroud)
它应该返回:
['This is a string spanning over multiple lines.\nAt somepoint there will be square brackets.','like this', 'And then maybe some more text.', 'And another text in square brackets']
Run Code Online (Sandbox Code Playgroud)
一个简短的例子:
data2 = 'A new string. [with brackets] another line [and a bracket]'
Run Code Online (Sandbox Code Playgroud)
我试过了:
re.split(r'(\[|\])', data2)
re.split(r'([|])', data2)
Run Code Online (Sandbox Code Playgroud)
但是这些会导致我的结果列表中的分隔符或完全错误的列表:
['A new string. ', '[', 'with brackets', ']', ' another line ', '[', 'and a bracket', ']', '']
Run Code Online (Sandbox Code Playgroud)
结果应该是:
['A new string.', 'with brackets', 'another line', 'and a bracket']
Run Code Online (Sandbox Code Playgroud)
作为特殊要求,应删除分隔符之前和之后的所有换行符和空格,并且不应包括在列表中.
>>> re.split(r'\[|\]', data2)
['A new string. ', 'with brackets', ' another line ', 'and a bracket', '']
Run Code Online (Sandbox Code Playgroud)
正如 arshajii 指出的那样,对于这个特定的正则表达式,您根本不需要组。
如果确实需要组来表达更复杂的正则表达式,则可以使用非捕获组进行拆分而不捕获分隔符。它可能对其他情况有用,但在这里语法上凌乱过度。
(?:……)
Run Code Online (Sandbox Code Playgroud)A non-capturing version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group cannot be retrieved after performing a match or referenced later in the pattern.
http://docs.python.org/2/library/re.html
所以这里不必要的复杂但具有示范性的例子是:
re.split(r'(?:\[|\])', data2)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3335 次 |
| 最近记录: |