[Tutor] Re: lists in re?

Andrei project5 at redrival.net
Tue Sep 9 01:55:47 EDT 2003



Andreas Zwinkau wrote:

 >Not exactly, i learned something from your example, but i thought about
 >something like
 >parser = re.compile(["[abc]","abc","[A-Z]"])

I'd write that pretty much like I wrote in the previous message, with a 
list comprehension:

parser = [ re.compile(item) for item in ["[abc]","abc","[A-Z]"] ]

 >parsedString = parser.replace(["-\\1-","def","[A-Z]"], string2parse)

I have no idea what this is supposed to do. What's string2parse and 
what's that list of strings in front of it?

 >lets say we have a dictionary for all replacement actions:
 >{"[abc]":"-\\1-", "abc":"def","[A-Z]":"_\\1_"}
 >how would you use this on "abBabcAbCC"?

Again, I don't understand what this dictionary contains. If you mean you 
have a dictionary of uncompiled regexes mapped to... something and you 
want to get those regexes compiled, you can extract them using the 
keys() property of the dictionary and combine that with the list 
comprehension I wrote above, like this:

 >>> test = {"[abc]":"-\\1-", "abc":"def","[A-Z]":"_\\1_"}
 >>> parser = [ re.compile(item) for item in test.keys() ]

Any chance of an example of what you're putting into it and what the 
code is supposed to make out of it? It would be most useful if you'd put 
in some *real* examples, because a key-value pair like "abc":"def" is a 
bit too abstract for me to understand its purpose.

Andrei



=====
Mail address in header catches spam. Real contact info (decode with rot13):
cebwrpg5 at bcrenznvy.pbz. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V 
ernq gur yvfg, fb gurer'f ab arrq gb PP.





More information about the Tutor mailing list