简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM