开发者

Regular Expression for HTML artifacts

开发者 https://www.devze.com 2022-12-08 03:56 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消