Python 获取两个字符串之间的字符串
我想在两个段落标记之间找到字符串。Python 获取两个字符串之间的字符串,python,django,Python,Django,我想在两个段落标记之间找到字符串。还有这个字符串 如何获取前两个段落标记之间的字符串?然后,如何获取第2段标记之间的字符串? 重新导入 matches=re.findall(r'+?',字符串) 以下是您在控制台中运行的文本 import re matches = re.findall(r'<p>.+?</p>',string) >>重新导入 >>>string=“”我想在两个段落标记之间找到字符串。还有这个字符串 >>>关于findall(“+?”,字符串) [“
还有这个字符串 如何获取前两个段落标记之间的字符串?然后,如何获取第2段标记之间的字符串?
重新导入
matches=re.findall(r'+?',字符串)
以下是您在控制台中运行的文本
import re
matches = re.findall(r'<p>.+?</p>',string)
>>重新导入
>>>string=“”我想在两个段落标记之间找到字符串。
还有这个字符串
>>>关于findall(“+?”,字符串)
[“我想在两个段落标记之间找到字符串。”,“还有这个字符串”]
介于和
>>>import re
>>>string = """<p>I'd like to find the string between the two paragraph tags.</p><br><p>And also this string</p>"""
>>>re.findall('<p>.+?</p>',string)
["<p>I'd like to find the string between the two paragraph tags.</p>", '<p>And also this string</p>']
[7]中的:content=“我想在两个段落标记之间找到字符串。
还有这个字符串
在[8]中:关于findall(r'(.+?),内容)
出[8]:
[“我想在两个段落标记之间找到字符串。”,
'还有这个字符串']
如果需要p标记之间的字符串(不包括p标记),请在findall方法中的+?中添加括号
In [7]: content = "<p>I'd like to find the string between the two paragraph tags.</p><br><p>And also this string</p>"
In [8]: re.findall(r'<p>(.+?)</p>', content)
Out[8]:
["I'd like to find the string between the two paragraph tags.",
'And also this string']
我已经给了你们一票,但请更正第一段中的拼写错误。应该是
,这听起来可能很愚蠢,但怎么会不起作用呢split=re.findall(“+?”,内容)
second=split[1]
…它给了我一个索引超出范围的错误。我怎样才能得到第二个元素?@Zorgan,你能改变一下内容吗?它在我的控制台中工作:>>split=re.findall(“(.+?)”,string)>>>split[1]”还有这个字符串“
是的,这是我的内容,我现在已经修复了它。谢谢,太好了。正是我需要的。
In [7]: content = "<p>I'd like to find the string between the two paragraph tags.</p><br><p>And also this string</p>"
In [8]: re.findall(r'<p>(.+?)</p>', content)
Out[8]:
["I'd like to find the string between the two paragraph tags.",
'And also this string']
import re
string = """<p>I'd like to find the string between the two paragraph tags.</p><br><p>And also this string</p>"""
subStr = re.findall(r'<p>(.+?)</p>',string)
print subStr
["I'd like to find the string between the two paragraph tags.", 'And also this string']