String 在Python中,有没有一种方法可以在字符串的两个单词之间创建子字符串?
我的问题或多或少类似于: 但它更具体。 如何得到位于初始字符串中两个已知单词之间的字符串的Par。< /P> 例如:String 在Python中,有没有一种方法可以在字符串的两个单词之间创建子字符串?,string,python-3.x,substring,String,Python 3.x,Substring,我的问题或多或少类似于: 但它更具体。 如何得到位于初始字符串中两个已知单词之间的字符串的Par。< /P> 例如: mySrting = "this is the initial string" Substring = "initial" 知道“the”和“string”是字符串中可用于获取子字符串的两个已知单词 谢谢大家! 您可以从这里的简单字符串操作开始。是您最好的朋友,因为它会告诉您子字符串在字符串中的位置;您也可以稍后在字符串中的某个位置开始搜索: >>> mySt
mySrting = "this is the initial string"
Substring = "initial"
知道“the”和“string”是字符串中可用于获取子字符串的两个已知单词
谢谢大家! 您可以从这里的简单字符串操作开始。是您最好的朋友,因为它会告诉您子字符串在字符串中的位置;您也可以稍后在字符串中的某个位置开始搜索:
>>> myString = "this is the initial string"
>>> myString.index('the')
8
>>> myString.index('string', 8)
20
查看片段[8:20]
,我们已经接近我们想要的:
>>> myString[8:20]
'the initial '
当然,既然我们找到了'the'
的起始位置,我们就需要考虑它的长度。最后,我们可能想去掉空白:
>>> myString[8 + 3:20]
' initial '
>>> myString[8 + 3:20].strip()
'initial'
结合起来,您可以这样做:
startIndex = myString.index('the')
substring = myString[startIndex + 3 : myString.index('string', startIndex)].strip()
如果要多次查找匹配项,则只需重复此操作,同时只查看字符串的其余部分。由于
str.index
只会找到第一个匹配项,因此您可以使用它非常有效地扫描字符串:
searchString = 'this is the initial string but I added the relevant string pair a few more times into the search string.'
startWord = 'the'
endWord = 'string'
results = []
index = 0
while True:
try:
startIndex = searchString.index(startWord, index)
endIndex = searchString.index(endWord, startIndex)
results.append(searchString[startIndex + len(startWord):endIndex].strip())
# move the index to the end
index = endIndex + len(endWord)
except ValueError:
# str.index raises a ValueError if there is no match; in that
# case we know that we’re done looking at the string, so we can
# break out of the loop
break
print(results)
# ['initial', 'relevant', 'search']
您可以从这里的简单字符串操作开始。是您最好的朋友,因为它会告诉您子字符串在字符串中的位置;您也可以稍后在字符串中的某个位置开始搜索:
>>> myString = "this is the initial string"
>>> myString.index('the')
8
>>> myString.index('string', 8)
20
查看片段[8:20]
,我们已经接近我们想要的:
>>> myString[8:20]
'the initial '
当然,既然我们找到了'the'
的起始位置,我们就需要考虑它的长度。最后,我们可能想去掉空白:
>>> myString[8 + 3:20]
' initial '
>>> myString[8 + 3:20].strip()
'initial'
结合起来,您可以这样做:
startIndex = myString.index('the')
substring = myString[startIndex + 3 : myString.index('string', startIndex)].strip()
如果要多次查找匹配项,则只需重复此操作,同时只查看字符串的其余部分。由于
str.index
只会找到第一个匹配项,因此您可以使用它非常有效地扫描字符串:
searchString = 'this is the initial string but I added the relevant string pair a few more times into the search string.'
startWord = 'the'
endWord = 'string'
results = []
index = 0
while True:
try:
startIndex = searchString.index(startWord, index)
endIndex = searchString.index(endWord, startIndex)
results.append(searchString[startIndex + len(startWord):endIndex].strip())
# move the index to the end
index = endIndex + len(endWord)
except ValueError:
# str.index raises a ValueError if there is no match; in that
# case we know that we’re done looking at the string, so we can
# break out of the loop
break
print(results)
# ['initial', 'relevant', 'search']
您也可以尝试以下方法:
mystring = "this is the initial string"
mystring = mystring.strip().split(" ")
for i in range(1,len(mystring)-1):
if(mystring[i-1] == "the" and mystring[i+1] == "string"):
print(mystring[i])
您也可以尝试以下方法:
mystring = "this is the initial string"
mystring = mystring.strip().split(" ")
for i in range(1,len(mystring)-1):
if(mystring[i-1] == "the" and mystring[i+1] == "string"):
print(mystring[i])
我建议结合使用
list、split
和join
方法。
如果要在子字符串中查找多个单词,这应该会有所帮助
words=list(string.split())
open=words.index('the')
close=words.index('string')
substring=''.join(单词[open+1:close])
如果您的问题变得更复杂,即成对值多次出现,我建议使用正则表达式
重新导入
substring=''.join(re.findall(r'the(+?)string',string))
如果在列表中查看子字符串,则re
应单独存储子字符串
我使用描述之间的空格来排除单词之间的空格,您也可以根据需要进行修改 我建议结合使用list、split
和join
方法。
如果要在子字符串中查找多个单词,这应该会有所帮助
将字符串转换为数组:
words=list(string.split())
获取开始和结束标记的索引,然后返回子字符串:
open=words.index('the')
close=words.index('string')
substring=''.join(单词[open+1:close])
在继续之前,您可能希望通过检查有效性来进行一些改进
如果您的问题变得更复杂,即成对值多次出现,我建议使用正则表达式
重新导入
substring=''.join(re.findall(r'the(+?)string',string))
如果在列表中查看子字符串,则re
应单独存储子字符串
我使用描述之间的空格来排除单词之间的空格,您也可以根据需要进行修改 那么你想要两个已知单词之间的字符串?为什么空格不是子字符串的一部分
?此外,如果'the'
和'string'
在mystring
中多次出现,该怎么办?@WillemVanOnsem则可能会显示字符串列表。@WillemVanOnsem和空格可以包含在另外两个词“the”和“string”中,因此您需要两个已知单词之间的字符串?为什么空格不属于子字符串
?此外,如果'the'
和'string'
在mystring
中多次出现,该怎么办?@WillemVanOnsem则可能会显示字符串列表。@WillemVanOnsem和空格可以包含在另外两个单词“the”和“string”中