0

I want to extract the testbed name from a file. This is the line where the name is present, How could I do it in Python?

Status: aaa (image='some_image_name' on testbed='test_bed_name' archiving)

I was thinking of splitting the string taking the index, but this line may not be the same in all my files. there might be some small changes.

1
  • Downvote: Please demonstrate your own effort so far; without code we can't really tell where you are stuck or how much you already know. Also, with a single sample, it's hard to generalize - multiple sample lines, and/or examples of things we should not include would focus the question significantly. Commented Jun 9, 2016 at 4:29

2 Answers 2

2

With a lookbehind.

>>> re.search("(?<=testbed=')([^']+)'", "Status: aaa (image='some_image_name' on testbed='test_bed_name' archiving)").groups()
('test_bed_name',)
Sign up to request clarification or add additional context in comments.

Comments

0

If the format is always like that, you could simply slice your way to the right place:

>>> s.split('=')[-1].split(' ')[0][1:-1]
test_bed_name

Or, you can use regular expressions:

>>> re.findall(r"(?:.*?)testbed='(.*?)'(?:.*?)", s)
test_bed_name

The above is a bit of a ham-fisted approach to regular expressions, but you can always tweak it out to make it a bit more precise.

3 Comments

Thanks, but I'm getting a syntax eror with the regular expression. I'm newbie. Could you please help?
Have you checked you are using the correct set of " ? Check the update, there was a small typo there.
Solved the issue. Thanks.

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.