Tom
Tom

Reputation: 34356

Python search for value in string

I have a string like this:

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

I'm trying to get the value of curr_items, so far I've got

out = telnet.read_until("END")
req = re.search("curr_items", out).group(0).split()[0]

Which returns curr_items, how do I get the value?

Thanks

Upvotes: 1

Views: 2581

Answers (3)

Taras
Taras

Reputation: 429

try:
    req = int(re.search("(?<=curr_items)\s*([\d]*)", out).group(0))
except:
    # No value was found.
    req = defaultValue

Upvotes: 1

khachik
khachik

Reputation: 28693

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

Upvotes: 1

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 262919

You can add a capture group matching the value to your regex:

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

Upvotes: 6

Related Questions