Split a string of a specific pattern into three parts

懵懂的女人 提交于 2021-01-27 06:53:02

问题


I am given a string which is of this pattern:

[blah blah blah] [more blah] some text

I want to split the string into three parts: blah blah blah, more blah and some text.

A crude way to do it is to use mystr.split('] '), and then removes the lead [ from the first two elements. Is there a better and performant way (need to do this for thousands of strings very quickly).


回答1:


You can use a regular expression to extract the text, if you know that it will be in that form. For efficiency, you can precompile the regex and then repeatedly use it when matching.

prog = re.compile('\[([^\]]*)\]\s*\[([^\]]*)\]\s*(.*)')

for mystr in string_list:
    result = prog.match(mystr)
    groups = result.groups()

If you'd like an explanation on the regex itself, you can get one using this tool.




回答2:


You can use a regular expression to split where you want to leave out characters:

>>> import re
>>> s = '[...] [...] ...'
>>> re.split(r'\[|\] *\[?', s)[1:]
['...', '...', '...']


来源:https://stackoverflow.com/questions/16701536/split-a-string-of-a-specific-pattern-into-three-parts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!