Kaspie
Kaspie

Reputation: 187

delete occurence in list with regex in python

So I'm reading an .avi file and storing it's index into a list. Each element corresponds to an index movie frame. I'm working on a script to delete all the occurrences in the list that start with :

00dc\x10\x00\x00\x00

Here's a short version of the code

   list = ['00dc\x00\x00\x00\x00\x025,\x00\x08\x19\x00\x00',
        '00dc\x00\x00\x00\x00\x12N,\x00\x0b6\x00\x00',
        '00dc\x10\x00\x00\x00&\x84,\x00\x95D\x01\x00',
        '00dc\x00\x00\x00\x00\xc4\xc8-\x00\xe0l\x00\x00',
        '00dc\x00\x00\x00\x00\xac5.\x00t.\x00\x00']

    regex1 = b'00dc\x10\x00\x00\x00.{8}'
    newlist = [x for x in list if x != regex1]

Aaand it doesn't do anything, the list stays the same when I expected the third element to be popped out.

I don't think it matches anything because even when I set the regex1 to :

b'.*'

The list stays the same. Having trouble figuring out where the issue is coming from. Thanks

Upvotes: 0

Views: 80

Answers (1)

Carles Mitjans
Carles Mitjans

Reputation: 4866

Python doesn't work with regex built-in. You need to import regex module.

import re
list = ['00dc\x00\x00\x00\x00\x025,\x00\x08\x19\x00\x00',
        '00dc\x00\x00\x00\x00\x12N,\x00\x0b6\x00\x00',
        '00dc\x10\x00\x00\x00&\x84,\x00\x95D\x01\x00',
        '00dc\x00\x00\x00\x00\xc4\xc8-\x00\xe0l\x00\x00',
        '00dc\x00\x00\x00\x00\xac5.\x00t.\x00\x00']

pattern = re.compile(b'00dc\x10\x00\x00\x00.{8}')
newlist = [x for x in list if not re.match(pattern,x)]

Output:

['00dc\x00\x00\x00\x00\x025,\x00\x08\x19\x00\x00', '00dc\x00\x00\x00\x00\x12N,\x00\x0b6\x00\x00', '00dc\x00\x00\x00\x00\xc4\xc8-\x00\xe0l\x00\x00', '00dc\x00\x00\x00\x00\xac5.\x00t.\x00\x00']

Upvotes: 1

Related Questions