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

编程入门 行业动态 更新时间:2024-10-24 01:52:22
本文介绍了在两个子字符串之间查找字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

我现在的方法是这样的:

>>>开始 = 'asdf = 5;'>>>结束 = '123jasd'>>>s = 'asdf = 5;iwantthis123jasd'>>>打印((s.split(start))[1].split(end)[0])我要这个

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

忘了说:该字符串可能不会以 start 和 end 开头和结尾.它们前后可能有更多字符.

解决方案

import res = 'asdf = 5;iwantthis123jasd'结果 = re.search('asdf=5;(.*)123jasd', s)打印(结果.组(1))

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))

更多推荐

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

本文发布于:2023-11-02 17:42:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553007.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   个子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!