[英]Python split string at spaces as usual, but keep certain substrings containing space?
Consider I have the following string:考虑我有以下字符串:
>>> aa="63452 [ 0] AAA BB CCC"
If I do the usual .split()
which splits at whitespaces, I get this:如果我执行通常在空格处拆分的
.split()
,我会得到:
>>> aa.split()
['63452', '[', '0]', 'AAA', 'BB', 'CCC']
What I'd want to obtain instead, is this list: ['63452', '[ 0]', 'AAA', 'BB', 'CCC']
我想要获得的是这个列表:
['63452', '[ 0]', 'AAA', 'BB', 'CCC']
Basically, the second part is a string that matches the format: opening square bracket + none or more of whitespace characters + none or more digits + closing square bracket - which I can match with this regex:基本上,第二部分是一个匹配格式的字符串:左方括号 + 没有或更多空白字符 + 没有或更多数字 + 右方括号 - 我可以用这个正则表达式匹配:
>>> import re
>>> re.findall(r'\[\s*\d*\]', aa)
['[ 0]']
In essence, I'd first want to identify the "square bracket" item, then split as .split()
usually does it, while keeping the "square bracket" item.本质上,我首先要识别“方括号”项,然后拆分为
.split()
通常这样做,同时保留“方括号”项。
So, what would be the most straightforward way, to obtain the desired list from the given string?那么,从给定字符串中获取所需列表的最直接方法是什么?
You can use an alternation pattern that matches a bracketed string or non-space characters:您可以使用与括号中的字符串或非空格字符匹配的交替模式:
re.findall(r'\[.*?]|\S+', aa)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.