re.search - just skip it

Fredrik Lundh fredrik at pythonware.com
Wed Jan 26 17:15:22 CET 2005


<rasdj at frontiernet.net> wrote:

> but output is:
>
> SET2_S_W CHAR(1) NOT NULL,
> SET4_S_W CHAR(1) NOT NULL,
>
> It should delete every, not every other!

for i in range(len(lines)):
    try:
        if s_ora.search(lines[i]): del lines[i]
    except IndexError:
        ...

when you loop over a range, the loop counter is incremented also if you delete
items.  but when you delete items, the item numbering changes, so you end up
skipping over an item every time the RE matches.

to get rid of all lines for which s_ora.search matches, try this

    lines = [line for line in lines if not s_ora.search(line)]

for better performance, get rid of the leading and trailing ".*" parts of your
pattern, btw.  not that it matters much in this case (unless the SQL state-
ment is really huge).

</F> 






More information about the Python-list mailing list