将特定模式的字符串分成三部分 [英] Split a string of a specific pattern into three parts

查看:40
本文介绍了将特定模式的字符串分成三部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个具有这种模式的字符串:

I am given a string which is of this pattern:

[blah blah blah] [more blah] some text

我想将字符串分成三部分:blah blah blahmore blahsome text.

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

一个粗略的方法是使用mystr.split('] '),然后从前两个元素中删除前导[.有没有更好更高效的方法(需要非常快速地对数千个字符串执行此操作).

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).

推荐答案

您可以使用正则表达式来提取文本,如果您知道它将采用该格式.为了效率,可以预编译正则表达式,然后匹配时重复使用.

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.

这篇关于将特定模式的字符串分成三部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆