While there are several posts on StackOverflow that are similar to this, none of them involve a situation when the target string is one space after one of the substrings.
I have the following string (example_string):
<insert_randomletters>[?] I want this string.Reduced<insert_randomletters>
I want to extract "I want this string." from the string above. The randomletters will always change, however the quote "I want this string." will always be between [?]
(with a space after the last square bracket) and Reduced.
Right now, I can do the following to extract "I want this string".
target_quote_object = re.search('[?](.*?)Reduced', example_string)
target_quote_text = target_quote_object.group(1)
print(target_quote_text[2:])
This eliminates the ]
and that always appear at the start of my extracted string, thus only printing "I want this string." However, this solution seems ugly, and I'd rather make
re.search()
return the current target string without any modification. How can I do this?
[
and?
, and add\s*
after]
,'\[\?]\s*(.*?)Reduced'
– Extremere.search('] (.*?)Reduced', example_string)
. Would my solution work too, or is yours more optimal? – Snowman[?
before]
, it will. – Extreme.group(1)
to avoid errors if no match is found. – Extreme