简体   繁体   中英

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:

>>> aa.split()
['63452', '[', '0]', 'AAA', 'BB', 'CCC']

What I'd want to obtain instead, is this list: ['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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM