开发者

Python regular expression not giving expected result

Trying to do a regular expression search on a list in python 2.5.4 - example code:

import re

list_in = ['heti_abcd_xyz_1234', 'heti_abcd_xyz', 'heti_abcd']

en = re.compile('abcd_xyz_1234$')

for item in list开发者_开发问答_in:
    if en.search(item) is None:
        list_in.remove(item)
print list_in

For the result however I get:

['heti_abcd_xyz_1234', 'heti_abcd']

when I'm expecting only the first element.

Any advice much appreciated.


You're modifying the list as you iterate over it. You can't do that.


It can be a lot simpler:

>>> list_in = ['heti_abcd_xyz_1234', 'heti_abcd_xyz', 'heti_abcd']
>>> [x for x in list_in if x.endswith("abcd_xyz_1234")]
['heti_abcd_xyz_1234']

If you want a solution that works for arbitrary regex, you can do:

pattern = re.compile("your_regex")
new_list = [x for x in list_in if pattern.search(x)]


You cannot modify a list while iterating over it. Simply create a new one:

import re
list_in = ['heti_abcd_xyz_1234', 'heti_abcd_xyz', 'heti_abcd']
en = re.compile('abcd_xyz_1234$')
new_list = [item for item in list_in if en.search(item) is not None]
print new_list

Btw, use .endswith() instead of a regex unless the regex in your code is just an example for something more complex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜