iterating over lines in a file

Cliff Crawford cjc26 at nospam.cornell.edu
Sun Jul 23 10:43:10 EDT 2000


* nobody <no at bo.dy> menulis:
| 
| >> i like it, i just wish its functions would be more consistent about
| >> what sort of regexps they want - either all compiled or all not
| >> compiled; i'm seeing some wanting one and some the other, for some
| >> reason. might be just my system, i suppose...
| 
| > The two should be interchangable..AFAIK wherever you can use a compiled
| > regexp, you can use an uncompiled one, and vice-versa.
| 
| that's what the documentation claims, but here's what i get:
| 
| >>> match=re.match(reg_c, test)
| [...]
| TypeError: argument 1: expected string, instance found
| >>> match=re.match(reg,test)
| >>>
| 
| the compiled one works in re.findall, though. i've no idea why this
| bites me in this way, but it's not too hard to work around, so...

Oh, right..I should've explained it better :)  A compiled regexp is an
object which has match() and search() methods, so instead of

    match=re.match(reg_c, test)

you want to do

    match=reg_c.match(test)

Also, in your example you'd probably want to use search() rather than
match(), which only succeeds if the regexp matches the beginning of the
string (i.e. it acts as if there is an implicit '^' at the beginning of
the regexp).

-- 
cliff crawford    -><-    http://www.people.cornell.edu/pages/cjc26/
                          Synaesthesia now!            icq 68165166



More information about the Python-list mailing list