Regular Expression for HTML artifacts
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.
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.
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.
精彩评论