Python在字符串中搜索值

2024-06-16 11:45:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一根这样的绳子:

STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
STAT reclaimed 0
END

我想知道货币的价值,到目前为止

^{pr2}$

它返回curr_项,如何获取值?在

谢谢


Tags: bytes货币itemsstatendtotal价值绳子
3条回答
try:
    req = int(re.search("(?<=curr_items)\s*([\d]*)", out).group(0))
except:
    # No value was found.
    req = defaultValue

可以向正则表达式添加与值匹配的capture group

>>> int(re.search("curr_items (\d+)", out).group(1))
0

re.search("curr_items [0-9]*", out).group(0).split()[1]

相关问题 更多 >