我如何找到两个子字符串('123STRINGabc' -> '字符串')之间的字符串?
我现在的方法是这样的:
>>> start = 'asdf=5;'
>>> end = '123jasd'
>>> s = 'asdf=5;iwantthis123jasd'
>>> print((s.split(start))[1].split(end)[0])
iwantthis
然而,这似乎非常低效且不符合python规则。有什么更好的方法来做这样的事情吗?
忘了说:
字符串可能不是以start和end开始和结束的。他们可能会有更多的字符之前和之后。
下面是一个函数,我做了返回一个字符串(s)之间的字符串string1和string2搜索列表。
def GetListOfSubstrings(stringSubject,string1,string2):
MyList = []
intstart=0
strlength=len(stringSubject)
continueloop = 1
while(intstart < strlength and continueloop == 1):
intindex1=stringSubject.find(string1,intstart)
if(intindex1 != -1): #The substring was found, lets proceed
intindex1 = intindex1+len(string1)
intindex2 = stringSubject.find(string2,intindex1)
if(intindex2 != -1):
subsequence=stringSubject[intindex1:intindex2]
MyList.append(subsequence)
intstart=intindex2+len(string2)
else:
continueloop=0
else:
continueloop=0
return MyList
#Usage Example
mystring="s123y123o123pp123y6"
List = GetListOfSubstrings(mystring,"1","y68")
for x in range(0, len(List)):
print(List[x])
output:
mystring="s123y123o123pp123y6"
List = GetListOfSubstrings(mystring,"1","3")
for x in range(0, len(List)):
print(List[x])
output:
2
2
2
2
mystring="s123y123o123pp123y6"
List = GetListOfSubstrings(mystring,"1","y")
for x in range(0, len(List)):
print(List[x])
output:
23
23o123pp123
s = "123123STRINGabcabc"
def find_between( s, first, last ):
try:
start = s.index( first ) + len( first )
end = s.index( last, start )
return s[start:end]
except ValueError:
return ""
def find_between_r( s, first, last ):
try:
start = s.rindex( first ) + len( first )
end = s.rindex( last, start )
return s[start:end]
except ValueError:
return ""
print find_between( s, "123", "abc" )
print find_between_r( s, "123", "abc" )
给:
123STRING
STRINGabc
我认为应该注意的是-根据需要的行为,您可以混合使用index和rindex调用,或者使用上述版本之一(它相当于regex(.*)和(.*?)组)。
您可以简单地使用这段代码或复制下面的函数。全都整齐地排在一条线上。
def substring(whole, sub1, sub2):
return whole[whole.index(sub1) : whole.index(sub2)]
如果按照如下方式运行该函数。
print(substring("5+(5*2)+2", "(", "("))
你可能会得到这样的输出:
(5*2
而不是
5*2
如果您希望在输出的末尾有子字符串,代码必须如下所示。
return whole[whole.index(sub1) : whole.index(sub2) + 1]
但如果不希望子字符串在末尾,则+1必须在第一个值上。
return whole[whole.index(sub1) + 1 : whole.index(sub2)]