Find a string between two substrings in Python when there is a space after the first substring

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?

+4
source share
5 answers

Your pattern '[?](.*?)Reduced'matches a literal ?, and then commits any 0+ characters other than line break characters, as small as possible to the first substring Reduced. This [?]is a character class formed with captive brackets, and ?inside the character class is the letter ?char. This is why your group 1 also contains a ]space.

[?], [ ?, . , ], , 1. \s* (0 ) \s+ (1 ).

re.search(r'\[\?]\s*(.*?)Reduced', example_string)

regex demo.

import re
rx = r"\[\?]\s*(.*?)Reduced"
s = "<insert_randomletters>[?] I want this string.Reduced<insert_randomletters>"
m = re.search(r'\[\?]\s*(.*?)Reduced', s)
if m:
    print(m.group(1))
# => I want this string.

Python.

+4

:

target_quote_object = re.search('] (.*?)Reduced', example_string)
target_quote_text = target_quote_object.group(1)
print(target_quote_text)

Wiktor .

+1

Regex , :

mystr = '<insert_randomletters>[?] I want this string.Reduced<insert_randomletters>'

res = mystr.split('Reduced')[0].split('] ')[1]

# 'I want this string.'
+1

[co]/[sho] uld Positive Lookbehind (?<=\[\?\]):

enter image description here

import re
pattern=r'(?<=\[\?\])(\s\w.+?)Reduced'

string_data='<insert_randomletters>[?] I want this string.Reduced<insert_randomletters>'

print(re.findall(pattern,string_data)[0].strip())

:

I want this string.
+1

Like any other answer, this may not be necessary. Or just too long for Python. This method uses one of the common string methods find.

  • str.find(sub,start,end)will return the index of the first occurrence subin the substring str[start:end]or will return -1 if it is not found.
  • In each iteration, the index [?]is retrieved with the index Reduced. The resulting substring is received.
  • Each time this pattern is returned [?]...Reduced, the index is updated to the rest of the row. Search continues on this index.

The code

s = ' [?] Nice to meet you.Reduced  efweww  [?] Who are you? Reduced<insert_randomletters>[?] I want this 
string.Reduced<insert_randomletters>'


idx = s.find('[?]')
while idx is not -1:
    start = idx
    end = s.find('Reduced',idx)
    print(s[start+3:end].strip())
    idx = s.find('[?]',end)

Output

$ python splmat.py
Nice to meet you.
Who are you?
I want this string.
0
source

Source: https://habr.com/ru/post/1695609/


All Articles