Python正则表达式

标签 python regex

str1 = abdk3<h1>The content we need</h1>aaaaabbb<h2>The content we need2</h2>

我们需要h1标签和h2标签里面的内容。

最好的方法是什么?谢谢

感谢您的帮助!

最佳答案

如果需要扩展,最好的方法是使用 BeautifulSoup 之类的东西。

>>> from BeautifulSoup import BeautifulSoup
>>> soup = BeautifulSoup('abdk3<h1>The content we need</h1>aaaaabbb<h2>The content we need2</h2>')
>>> soup.h1
<h1>The content we need</h1>
>>> soup.h1.text
u'The content we need'
>>> soup.h2
<h2>The content we need2</h2>
>>> soup.h2.text
u'The content we need2'

它也可以用正则表达式来完成,但这可能更符合你的需要。你想要的一个更大的例子可能很好。如果不完全了解您想要解析的内容,就很难提供适当的帮助。

关于Python正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4182331/

相关文章:

python - Pandas Python - 转换 HH :MM:SS into seconds in aggegate (csv file)

python - 如何延迟时间?

java - 匹配正则表达式中从后面开始的第一次出现

ruby - 匹配行首或空格字符后的模式的正则表达式

java - 如何禁止 "."(点)作为正则表达式中的第一个字符

java - 在Java中正确使用Regex,Regex不匹配

python - 在 python 问题中查找与 .csv 文件的匹配项

python - 使用 scons 执行验证

python - 运行代码时更新 kivy 小部件的属性

c# - 匹配运算符