1

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

1
  • What value to yo expect to get? Commented Dec 9, 2010 at 10:05

3 Answers 3

6

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

>>> int(re.search("curr_items (\d+)", out).group(1))
0
Sign up to request clarification or add additional context in comments.

3 Comments

Is this a better solution than the one submitted by khachik?
@Tom, depends on what you mean by "better". My answer uses the regex to do all the work, so I can spare a call to split() and a list item access. @khachik's solution supports lines with no value (since he uses * instead of + in the regex) but his list access (.split()[1]) will fail in that case. Both solutions are good starting points, though :)
Better = best practice in python. Thanks for your detailed answer
1

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.