-1

如果我有一个像这样的字符串:

str = 'Hello, <code>This is the string i want to extract</code>'

那么我将如何提取介于<code>和之间的字符串</code>,在上述情况下,提取字符串是:

'This is the string i want to extract'

我想在 django 过滤器中使用这个字符串。

4

2 回答 2

4

使用解析器,例如BeautifulSoup

>>> from bs4 import BeautifulSoup as BS
>>> text = 'Hello, <code>This is the string i want to extract</code>'
>>> soup = BS(text)
>>> print soup.code.text
This is the string i want to extract

或者,如果只有一行,您可以只使用正则表达式:

>>> import re
>>> re.search(r'<code>(.*?)</code>', text).group(1)
'This is the string i want to extract'

顺便说一句,请不要命名字符串str。它将覆盖内置类型。

于 2013-10-21T07:32:40.390 回答
1

试试这个,如果你也想要“你好”

from bs4 import BeautifulSoup
import re
sentence = 'Hello, <code>This is the string i want to extract</code>'   
print re.sub('<[^>]*>', '',  sentence)

Hello, This is the string i want to extract
于 2013-10-21T08:16:02.587 回答