我有一个这样的字符串:
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
STAT reclaimed 0
END
我正在尝试获取curr_items的值,到目前为止我已经
了out = telnet.read_until("END")
req = re.search("curr_items", out).group(0).split()[0]
返回curr_items,如何获取值?
由于
答案 0 :(得分:6)
您可以添加与您的正则表达式匹配的capture group:
>>> int(re.search("curr_items (\d+)", out).group(1))
0
答案 1 :(得分:1)
re.search("curr_items [0-9]*", out).group(0).split()[1]
答案 2 :(得分:1)
try:
req = int(re.search("(?<=curr_items)\s*([\d]*)", out).group(0))
except:
# No value was found.
req = defaultValue