1

Eg-My string is :

str1="[00:00:0.047] [TestRunner] [Main] [sleep_assoc] > [Iteration:0][00:00:0.063] [TestRunner] [Main] [sleep_assoc] > [!Iteration:0][00:00:0.063]"

I want to store all characters between [Iteration:0] & [!Iteration:0] to a string variable.

2
  • I don't suppose you tried anything yourself? Commented Mar 3, 2017 at 11:30
  • Hello, please provide an example of code you tried, and an example of input and the output you expect to have. Commented Mar 3, 2017 at 11:33

2 Answers 2

1

You can do this using .*?, that matches all characters from [Iteration:0] till it find [!Iteration:0]:

In [1]: import re

In [2]: s = '[00:00:0.047] [TestRunner] [Main] [sleep_assoc] > [Iteration:0][00:00:0.063] [TestRunner] [Main] [sleep_as
   ...: soc] > [!Iteration:0][00:00:0.063]'

In [3]: m = re.search(r'\[Iteration:0](.*?)\[!Iteration:0]', s)

In [4]: res = m.group(1)

In [5]: res
Out[5]: '[00:00:0.063] [TestRunner] [Main] [sleep_assoc] > '
Sign up to request clarification or add additional context in comments.

Comments

0

You don't need regex if the strings are so simple.

just do a str1.split('[Iteration:0]')[1].split('[!Iteration:0]')[0]

do you understand the code or shall I explain?

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.