Although there are a few posts in StackOverflow that look like this, none of them relate to the situation where the target line is one space after one of the substrings.
I have the following line (example_string):
<insert_randomletters>[?] I want this string.Reduced<insert_randomletters>
I want to extract "I want this line." from the line above. Random characters will always change, however the quote is “I want this line”. will always be between [?](with a space after the last square bracket) and reduced.
Currently, I can do the following to extract "I want this line."
target_quote_object = re.search('[?](.*?)Reduced', example_string)
target_quote_text = target_quote_object.group(1)
print(target_quote_text[2:])
This excludes ]and that always appear at the beginning of my extracted line, so only “I want this line” is printed. However, this solution seems ugly, and I would rather make it re.search()return the current target string without any changes. How can i do this?
source
share