Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试解析以下形式的字符串:
XXX XXXX XXXXXX XX XXXXXXXXXXXXX XXX
目标是捕获此字符串中所有可变长度的空格组。我将如何使用正则表达式来做到这一点?
import re re.findall(r'\s+', 'XXX XXXX XXXXXX XX XXXXXXXXXXXXX XXX')
这使:[' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ']
r'\s+'表示捕获任意组的空白字符(1 个或多个)。如果您需要严格的空格,请将其替换为r' +'.
r'\s+'
r' +'
re.findall在字符串中查找所有不重叠的匹配项。
re.findall