Python在string中搜索值

时间:2010-12-09 10:00:34

标签: python regex string

我有一个这样的字符串:

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,如何获取值?

由于

3 个答案:

答案 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