string - 在两个子字符串之间查找字符串

标签 string python substring

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

我现在的方法是这样的:

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

但是,这似乎非常低效且不符合 Python 风格。有什么更好的方法来做这样的事情?

忘了说: 字符串可能不以 startend 开头和结尾。它们前后可能有更多字符。

最佳答案

import re

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

关于string - 在两个子字符串之间查找字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3368969/

相关文章:

python - 以类似于 continue 的方式跳过 Python 迭代器 __next__ 中的迭代

python - selenium:使用 selenium 根据表单名称将数据提取到数据框

string - 大小为 n 的最大重复子串

C获取字符串中的一部分字符串

python - 如何避免在 Python 中对大量 dict 键字符串进行硬编码

c++ - Qt和std::string的通用高效修剪算法是什么?

python - Python 中的嵌套类

javascript - 使用 JavaScript 从字符串中获取整数

javascript - 在javascript中拆分字符串

python - 用条件替换字符串会产生奇怪的结果