如果我有一个像:
这样的字符串str = 'Hello, <code>This is the string i want to extract</code>'
那我将如何提取<code>
和</code>
之间的字符串,在上面的例子中,提取字符串是:
'This is the string i want to extract'
我想在django过滤器中使用此字符串。
答案 0 :(得分: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
。它将覆盖内置类型。
答案 1 :(得分: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