Python regex string split ()

I am new to regex in python. I have the following line and you want to break them into five categories. I just use split (), but it just breaks into 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 regex to make it broken like this:

  ["1", "0", "A10B", "1/00", "Description: This is description with spaces"] 

Can anyone help? Thanks!

+4
source share
3 answers

You can simply specify the number of sections:

 s.split(' ', 4) 
+10
source

The second argument to split() is the maximum number of partitions to run. If you set the value to 4, the remaining line will be in list 5.

  sp = s.split(' ', 4) 
+2
source

Not a perfect solution. But for starters.

 >>> sp=s.split()[0:4] >>> sp.append(' '.join(s.split()[4:])) >>> print sp ['1', '0', 'A10B', '1/00', 'Description: This is description with spaces'] 
+1
source

Source: https://habr.com/ru/post/1413685/


All Articles