How do I find a string between two substrings ('123STRINGabc' -> 'STRING')?

My current method is like this:

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

However, this seems very inefficient and un-pythonic. What is a better way to do something like this?

Forgot to mention: The string might not start and end with start and end. They may have more characters before and after.

推荐答案

import re

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

Python相关问答推荐

通过优化空间在Python中的饼图中添加标签

比较2 PD.数组的令人惊讶的结果

Django mysql图标不适用于小 case

发生异常:TclMessage命令名称无效.!listbox"

如何制作10,000年及以后的日期时间对象?

使用@ guardlasses. guardlass和注释的Python继承

在vscode上使用Python虚拟环境时((env))

* 动态地 * 修饰Python中的递归函数

matplotlib + python foor loop

Pandas:计算中间时间条目的总时间增量

重置PD帧中的值

替换现有列名中的字符,而不创建新列

Pandas:填充行并删除重复项,但保留不同的值

如何合并具有相同元素的 torch 矩阵的行?

Pandas 数据帧中的枚举,不能在枚举列上执行GROUP BY吗?

如何使用matplotlib查看并列直方图

如何在Python中解析特定的文本,这些文本包含了同一行中的所有内容,

文本溢出了Kivy的视区

有什么方法可以在不对多索引DataFrame的列进行排序的情况下避免词法排序警告吗?

GEKKO中若干参数的线性插值动态优化