Python 获取两个字符串中的子字符串

Python 获取两个字符串中的子字符串,python,regex,string,python-2.7,truncate,Python,Regex,String,Python 2.7,Truncate,我有一个非常非常大的字符串,其中包含某个系统的日志 我只想要以开头和结尾的部分。 我听说正则表达式是一种很好的方法,但我真的不知道如何使用它。 有什么想法吗?如果和只出现一次,那么可以使用string\u name[string\u name.index+8:string\u name.index] s = "Hello I am a very long string <status>I've got a lovely bunch of coconuts</status>

我有一个非常非常大的字符串,其中包含某个系统的日志 我只想要以开头和结尾的部分。 我听说正则表达式是一种很好的方法,但我真的不知道如何使用它。
有什么想法吗?

如果和只出现一次,那么可以使用string\u name[string\u name.index+8:string\u name.index]

s = "Hello I am a very long string <status>I've got a lovely bunch of coconuts</status> here they are standing in a row"
excerpt = s.partition("<status>")[2].rpartition("</status>")[0]
print excerpt

如果您想尝试正则表达式,以下是一种方法:

import re

regex = re.compile(r"\<status\>(.*?)\</status\>", re.IGNORECASE)
s = """This is some long random text <status>This is the first status block</status> 
and some more text <status>and another block</status> 
and yet more <status>This is the last status block</status>"""
print(re.findall(regex, s))

这种方法的主要优点是它可以提取所有。。。一条线上的块,而不仅仅是第一个。请注意,对于三重引号的字符串,和必须在同一行。

如果文本处于状态,您希望这样做吗?@S我想在其中随机打印一行以供个人使用,您应该更新您的问题以反映这一点。请注意,如果有多个带有。。。。但如果将整个文本加载到内存中,这肯定是完成此任务最有效的方法
s = "test<status>test2</status>"
print s[s.index("<status>") + 8: s.index("</status>"]
test2
import re

regex = re.compile(r"\<status\>(.*?)\</status\>", re.IGNORECASE)
s = """This is some long random text <status>This is the first status block</status> 
and some more text <status>and another block</status> 
and yet more <status>This is the last status block</status>"""
print(re.findall(regex, s))
['This is the first status block', 'and another block', 'This is the last status block']