Python 如何剪切匹配的字符串
我想剪下匹配的字符串Python 如何剪切匹配的字符串,python,string,substring,Python,String,Substring,我想剪下匹配的字符串 我考虑在[R.FixITER('')]中使用[m启动()]来获得GET索引。< /P> 但我认为存在的方式比这更好 例如,我想剪切“页眉”和“页脚”之间的字符串 请告诉我 import re def returnmatches(text,headers,footers): """headers is a list of headers footers is a list of footers text is the text to search""" fo
我考虑在[R.FixITER('')]中使用[m启动()]来获得GET索引。< /P> 但我认为存在的方式比这更好
例如,我想剪切“页眉”和“页脚”之间的字符串 请告诉我import re
def returnmatches(text,headers,footers):
"""headers is a list of headers
footers is a list of footers
text is the text to search"""
for header,footer in zip(headers,footers):
pattern = r"{}\w+?{}".format(header,footer)
try:
yield re.search(pattern,input_text).group()
except AttributeError:
# handle no match
pass
或者:
text = "header1svdijfooter1ccsdheader2cdijhfooter2"
headers = ["header1", "header2"]
footers = ["footer1", "footer2"]
import re
matches = [re.search(r"{}\w+?{}".format(header,footer),text).group() for header,footer in zip(headers,footers) if re.search(r"{}\w+?{}".format(header,footer),text)]
可以使用列表在一行中完成计算:
s = "header1svdijfooter1ccsdheader2cdijhfooter2"
headers = {"one": "header1", "two": "header2"}
footers = {"one": "footer1", "two": "footer2"}
out = [re.search('({}.*?{})'.format(headers[k], footers[k]), s).group(0) for k in sorted(headers.keys())]
如上所述,根据示例,假设存在且仅存在一个匹配组
或者,如果喜欢循环:
s = "header1svdijfooter1ccsdheader2cdijhfooter2"
headers = {"one": "header1", "two": "header2"}
footers = {"one": "footer1", "two": "footer2"}
out=[]
for k in sorted(headers.keys()):
out.extend(re.search('({}.*?{})'.format(headers[k], footers[k]), s).groups())
print out
上述操作产生输出:
['header1svdijfooter1', 'header2cdijhfooter2']
无需回复:
str = "header1svdijfooter1ccsdheader2cdijhfooter2"
result = []
capture=False
currentCapture = ""
for i in range(len(str)):
if str[i:].startswith("header1") or str[i:].startswith("header2"):
currentCapture = ""
capture=True
elif str[:i].endswith("footer1") or str[:i].endswith("footer2"):
capture=False
result.append(currentCapture)
currentCapture = ""
if capture:
currentCapture = currentCapture+str[i]
if currentCapture:
result.append(currentCapture)
print result
输出:
['header1svdijfooter1', 'header2cdijhfooter2']
请编辑您的帖子以修复
页眉
和页脚
。以这种方式声明它们是一个编译错误。您的意思是创建字典吗?使用dict()
或{}
这是正则表达式的一个很好的用法。你会考虑编辑你的答案来包含格式化的字符串,而不使用<代码> %<代码>格式操作符吗?没有提到空格在比赛中是特别的?@dementedhedgehog他的例子中没有任何空格,所以我不想在他的例子中包括任何他没有明确提到的内容。
str = "header1svdijfooter1ccsdheader2cdijhfooter2"
result = []
capture=False
currentCapture = ""
for i in range(len(str)):
if str[i:].startswith("header1") or str[i:].startswith("header2"):
currentCapture = ""
capture=True
elif str[:i].endswith("footer1") or str[:i].endswith("footer2"):
capture=False
result.append(currentCapture)
currentCapture = ""
if capture:
currentCapture = currentCapture+str[i]
if currentCapture:
result.append(currentCapture)
print result
['header1svdijfooter1', 'header2cdijhfooter2']