Regular expressions (in portal transform)

This is not too Plone specific, but maybe someone can help me:

I am making a portal transform, And I wonder if there are some libs or 'help stuff' that I can use for regular expressions. Also, I wonder if I have to 'go through the text several times to do the following:

1. every line starting with  @  should be removed
2. Every line staring with  @h1:   should be transformed to '<h1>the whole line</h1>
3.  Every line staring with @hx:  (h2, h3, h4 etc) should be transformed to '<hx>the whole line</hz>
4. <B>bold<$> should be transformed to <b>bold</b>
5. <I>italic<$> should be transformed to <i>italic</i> 
6. <B>bold<BI>italic<BI>bold<I>BoldItalic<$> should be transformed to:
   <b>bold</b><i>italic</i><b>bold><i>BoldItalic</i></b>  
   ($ 'turns of all styles, bold, italic, underlined etc, so it should close any tag that was opened on that line)

bold italic bold italic

Update: so basically, every occurrence of a tag turns it on/off (first time <b> is bold. Next time <b> is </b>, $ turns off every open tag, (and would 'reset' the tag, meaning that next <b> is bold.

Use the re module of :snake:

I found someone else working on something related, so to share I ended up with:

pypeg2