[Tutor] lists in re?

Andreas Zwinkau andi at buxach.de
Sat Sep 6 19:17:54 EDT 2003


Ave

I'm currently working on something wiki-like and i use regular
expressions very much to format the content. The bad part is, it gets
worse, so i'd thought about making it more elegant. Is there any way to
make replacements with re and use lists or a dictionary?

Here is the code:

	linkparser = re.compile("\[([\w \=\-\_\.]+)\]")
        listparser = re.compile("(?<=\n)\* (.*)\n")
        italicparser = re.compile("''(.+?)''")
        underlineparser = re.compile("__(.+?)__")
        boldparser = re.compile("\*\*(.+?)\*\*")
        headlineparser = re.compile("\!(.+?)\n")
        filtered = string.replace(raw_data, "\n","<br />\n")
        filtered = linkparser.sub("<a href=\"\\1\">\\1</a>", filtered)
        filtered = listparser.sub("<ul><li>\\1</li></ul>\n", filtered)
        filtered = italicparser.sub("<i>\\1</i>", filtered)
        filtered = underlineparser.sub("<u>\\1</u>", filtered)
        filtered = boldparser.sub("<b>\\1</b>", filtered)
        filtered = headlineparser.sub("<h3>\\1</h3>", filtered)

I checked the wikinehesa, but there it doesn't use re at all.

-- 
mfg
Andreas Zwinkau
 | web: andi.dasstellenwirinsinternet.de
 | mail: andi at buxach.de
 | jabber: beza1e1 at amessage.de



More information about the Tutor mailing list