tags:

views:

77

answers:

2

Hi,

I some text with HTML artifacts where the < and > of tags got dropped, so now I need something that will match a small p followed by a capital letter, like

pThe next day they....

And I also need something that will catch the trailing /p which is easier. These need to be stripped, i.e. replaced with "" in python.

What RE would I use for that? Thanks! Stephan.

+1  A: 

Try this:

re.sub(r"(/?p)(?=[A-Z]|$)", r"<\1>", str)

You might want to extend the boundary assertion (here (?=[A-Z]|$)) with additional characters like whitespace.

Gumbo
+1  A: 

I got is. You use backreferences,

import re
smallBig = re.compile(r'[a-z]([A-Z])')

...
cleanedString = smallBig.sub(r'\1', dirtyString)

This removes the small letter but keeps the capital letter in cases where the '<' and '>' of html tags were stripped and you sit with text like

pSome new paragraph text /p

Quick and dirty but it works in my case.

gouwsmeister