简体   繁体   English

Python 像往常一样在空格处拆分字符串,但保留某些包含空格的子字符串?

[英]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.

相关问题 在 Python 中用空格分割字符串——保留带引号的子字符串 - Split a string by spaces -- preserving quoted substrings -- in Python 在Python中按空格将字符串拆分为最大长度的子字符串 - Split string by spaces into substrings with max length in Python 使用 Python 中的子字符串拆分字符串 - Split string with substrings in Python 从特定字符开始按空格拆分python字符串 - Split python string by space starting at certain character 删除 Python 中字符串中多余空格并在单词之间保留 1 个空格的代码 - Code to remove extraneous spaces in a string in Python and keep 1 space between words Python 用空格分割字符串,除非在引号中,但保留引号 - Python split string by spaces except when in quotes, but keep the quotes 通过python将一个大字符串拆分为多个包含'n'个字的子字符串 - Split a large string into multiple substrings containing 'n' number of words via python Python:如何根据空间拆分字符串但保留'\\ n'? - Python: how to split a string based on space but keep '\n'? 通过保留引用的子串的空格将N个元素拆分为一个字符串 - Split into N elements a string by spaces preserving quoted substrings 如何拆分字符串并将其子字符串与子字符串列表相匹配? - Python - How to split a string and match its substrings to a list of substrings? - Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM