Python正则表达式

use*_*652 1 python regex

str1 = abdk3<h1>The content we need</h1>aaaaabbb<h2>The content we need2</h2>
Run Code Online (Sandbox Code Playgroud)

我们需要h1标签和h2标签内的内容.

最好的方法是什么?谢谢

谢谢您的帮助!

Chr*_*gan 6

如果它需要扩展的最佳方式是使用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'
Run Code Online (Sandbox Code Playgroud)

它也可以使用正则表达式完成,但这可能更符合您的要求.你想要的更好的例子可能是好的.如果不知道你想解析什么,很难正确地帮助它.