sdaau
sdaau

Reputation: 38619

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?

Upvotes: 1

Views: 139

Answers (1)

blhsing
blhsing

Reputation: 106455

You can use an alternation pattern that matches a bracketed string or non-space characters:

re.findall(r'\[.*?]|\S+', aa)

Upvotes: 1

Related Questions