说我有一句话如:
这只鸟在夜间飞行,翼展非常大。
我的目标是拆分字符串,以便结果出现:
并且有一个非常大的翼
我尝试过使用split()
,但是,我的努力还没有成功。如何将字符串拆分成片段,并删除字符串的开头部分和结束部分?
答案 0 :(得分:0)
foo
出:
import re
text = 'The bird flies at night and has a very large wing span.'
l = re.split(r'.+?(?=and)|(?<=wing).+?', text)[1]
答案 1 :(得分:0)
我想这是做你想做的最好的方法:
s = "The bird flies at night and has a very large wing span."
and_position = s.find("and") # return the first index of "and" in the string
wing_position = s.find("wing") # similar to the above
result = s[and_position:wing_position+4] # this is called python's slice
如果您不熟悉python切片,请在here了解更多信息。