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

sgharvey KephnosAnagennao at gmail.com
Sat Mar 22 16:27:49 EDT 2008


... 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?

Doesn't match properly:
<code>
      # Iterate through each pattern for each line
      for line in lines:
         for pattern in patterns:
            # Match each pattern to the current line
            match = patterns[pattern].search(line)
            if match:
               "%s: %s" % (pattern, str(match.groups()) )
</code>

_Does_ match properly:
<code>
      # Let's iterate through all the lines for each pattern
      for pattern in pattern:
         for line in lines:
            # Match each pattern to the current line
            match = patterns[pattern].search(line)
            if match:
               "%s: %s" % (pattern, str(match.groups()) )
</code>

Related code:
The whole src
http://pastebin.com/f63298772
regexen and delimiters (imported into whole src)
http://pastebin.com/f485ac180




More information about the Python-list mailing list