[英]Python: How to extract a string between two strings in reverse
I have a string like this,我有一个这样的字符串,
s = "'B_NAME:Basin Name', 'Z_NAME:Zone Name', 'MD:Min Depth', 'F_BLOCK:First Block'"
I want to extract string between ":Min Depth"
and previous single quote ( '
).我想在
":Min Depth"
和前一个单引号 ( '
) 之间提取字符串。 In the above example, the output I expect is "MD"
.在上面的例子中,我期望的 output 是
"MD"
。 What is the best way to do this in Python, appreciate your support.在 Python 中执行此操作的最佳方法是什么,感谢您的支持。
You should use regular expressions, something like this:您应该使用正则表达式,如下所示:
import re
re.findall(r'(?<=\')\w+(?=\:)', s)
# OUTPUT
# ['B_NAME', 'Z_NAME', 'MD', 'F_BLOCK']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.