3

I'm using python2 and I want to get rid of these empty strings in the output of the following python regular expression:

import re
x = "010101000110100001100001"
print re.split("([0-1]{8})", x)

and the output is this :

['', '01010100', '', '01101000', '', '01100001', '']

I just want to get this output:

['01010100', '01101000', '01100001']
Cœur
  • 37,241
  • 25
  • 195
  • 267

6 Answers6

4

Regex probably isn't what you want to use in this case. It seems that you want to just split the string into groups of n (8) characters.

I poached an answer from this question.

def split_every(n, s):
    return [ s[i:i+n] for i in xrange(0, len(s), n) ]

split_every(8, "010101000110100001100001")
Out[2]: ['01010100', '01101000', '01100001']
Community
  • 1
  • 1
Tim
  • 3,675
  • 12
  • 25
  • I actually made another code similar to this and I didn't get to know the Dynamic looping stuff yet, but it's really awesome :D – Ahmed Ramadan Dec 15 '16 at 04:33
2

One possible way:

print filter(None, re.split("([0-1]{8})", x))
Nurjan
  • 5,889
  • 5
  • 34
  • 54
1
import re
x = "010101000110100001100001"
l = re.split("([0-1]{8})", x)
l2 = [i for i in l if i]

out:

['01010100', '01101000', '01100001']
宏杰李
  • 11,820
  • 2
  • 28
  • 35
1

This is exactly what is split for. It is split string using regular expression as separator.

If you need to find all matches try use findall instead:

import re
x = "010101000110100001100001"
print(re.findall("([0-1]{8})", x))
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
1
print([a for a in re.split("([0-1]{8})", x) if a != ''])
G. Bahaa
  • 285
  • 4
  • 9
0

Following your regex approach, you can simply use a filter to get your desired output.

import re
x = "010101000110100001100001"
unfiltered_list = re.split("([0-1]{8})", x)
print filter(None, unfiltered_list)

If you run this, you should get:

['01010100', '01101000', '01100001']
gom1
  • 150
  • 8