re.search (works)|(doesn't work) depending on for loop order

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat Mar 22 17:21:39 EDT 2008


On Sat, 22 Mar 2008 13:27:49 -0700, sgharvey wrote:

> ... and by works, I mean works like I expect it to.
> 
> I'm writing my own cheesy config.ini parser because ConfigParser
> doesn't preserve case or order of sections, or order of options w/in
> sections.
> 
> What's confusing me is this:
>    If I try matching every line to one pattern at a time, all the
> patterns that are supposed to match, actually match.
>    If I try to match every pattern to one line at a time, only one
> pattern will match.
> 
> What am I not understanding about re.search?

That has nothing to do with `re.search` but how files work.  A file has a
"current position marker" that is advanced at each iteration to the next
line in the file.  When it is at the end, it stays there, so you can just
iterate *once* over an open file unless you rewind it with the `seek()`
method.

That only works on "seekable" files and it's not a good idea anyway
because usually the files and the overhead of reading is greater than the
time to iterate over in memory data like the patterns.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list