Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regular expression split() string

I'm quite new to regular expressions in Python. I have the following string and want to split them into five categories. I just use the split(), but it will just split according to white spaces.

s = "1 0 A10B 1/00 Description: This is description with spaces"
sp = s.split()
>>> sp
["1", "0", "A10B", "1/00", "Description:", "This", "is", "description", "with", "spaces"]

How can I write a regular expression to make it split like the following?

 ["1", "0", "A10B", "1/00", "Description: This is description with spaces"]
like image 362
hash__x Avatar asked Dec 13 '22 02:12

hash__x


1 Answers

You may simply specify a number of splits:

s.split(' ', 4)
like image 180
Roman Bodnarchuk Avatar answered Dec 22 '22 01:12

Roman Bodnarchuk