简体   繁体   English

Python正则表达式; 在最后一个实例上匹配

[英]Python regular expression; match on the last instance

I have a bunch of html I am trying to deal with. 我有一堆正在尝试处理的html。 I want to delete the last half tag that I have. 我想删除我的最后一半标签。 Basically I am starting with: 基本上我是从:

</div></div><div class="_3o-d" id="education

and want to end with: 并希望以:

</div></div>

I tried: 我试过了:

workSection = re.split('<.*?$',workSection)[0]

but this matches the first '<' and leaves me with an empty string. 但这与第一个'<'相匹配,并给我留下了一个空字符串。 Is there a way to just match the last instance? 有没有办法只匹配最后一个实例? Or to somehow start from the end? 还是以某种方式从头开始?

I am also aware that splitting and then taking the first option may not be the best way of doing this, and am prepared to take a beating for it now. 我也知道,先拆分然后采取第一个选择可能不是这样做的最佳方法,并且准备立即采取行动。

Just use [^<] instead of the . 只需使用[^<]代替即可.

>>> re.split('<[^<]*$', '</div></div><div class="_3o-d" id="education')
['</div></div>', '']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM