小编典典

Python-查找两个子字符串之间的字符串

python

如何找到两个子字符串('123STRINGabc' -> 'STRING')之间的字符串?

我当前的方法是这样的:

>>> start = 'asdf=5;'
>>> end = '123jasd'
>>> s = 'asdf=5;iwantthis123jasd'
>>> print((s.split(start))[1].split(end)[0])
iwantthis

但是,这似乎效率很低而且不合Python。什么是做这样的更好的方法?

忘了提:该字符串可能无法启动,并最终startend。他们之前和之后的字符可能更多。


阅读 802

收藏
2020-02-23

共1个答案

小编典典

import re

s = 'asdf=5;iwantthis123jasd'
result = re.search('asdf=5;(.*)123jasd', s)
print(result.group(1))
2020-02-23