Skip to content
Advertisement

Why does this regex capture a maximum of 2 capture groups and not all those within the input string?

import re

def verify_need_to_restructure_where_capsule(m):
    capture_where_capsule = str(m.group(1))
    print(capture_where_capsule)
    return capture_where_capsule


input_text = "Rosa está esperándote ((PL_ADVB='saassa')abajo). Estábamos ((PL_ADVB='la casa cuando comenzó el temporal')dentro). Los libros que buscas están ((PL_ADVB='en la estantería de allí arriba')arriba); Conociéndole, quizás ya tenga las cosas preparadas ((PL_ADVB='mente mesa principal, ademas la hemos arreglado')sobre)"


list_all_adverbs_of_place = ["adentro", "dentro", "arriba de", "arriba", "al medio", "abajo", "hacía", "hacia", "por sobre", "sobre las","sobre la", "sobre el", "sobre"]
place_reference = r"(?i:ws*)+"
pattern = re.compile(r"(((PL_ADVB='" + place_reference + r"')" + rf"({'|'.join(list_all_adverbs_of_place)})" + r"))", re.IGNORECASE)

input_text = re.sub(pattern, verify_need_to_restructure_where_capsule, input_text, re.IGNORECASE)

Even if you try with several input_text in all cases it is limited (at most) to capture the first 2 matches, but not all the occurrences that actually exist

((PL_ADVB='saassa')abajo)
((PL_ADVB='la casa cuando comenzó el temporal')dentro)

This should be the correct output, that is, when it succeeds in identifying all occurrences and not just the first 2 matches.

((PL_ADVB='saassa')abajo)
((PL_ADVB='la casa cuando comenzó el temporal')dentro)
((PL_ADVB='en la estantería de allí arriba')arriba)
((PL_ADVB='mente mesa principal, ademas la hemos arreglado')sobre)

It’s quite curious because if I invert the order of the capture groups within the string, the pattern will detect them, but always limited to the first 2. It is as if the re.sub() method had passed the parameter to replace n number of times (in this case like 2 times), but in that case I am not indicating that parameter, and even so re.sub() just works a limited number of times.


EDIT (with findall):

import re

def verify_need_to_restructure_where_capsule(m):
    capture_where_capsule = str(m.group(1))
    print(capture_where_capsule)
    return capture_where_capsule


input_text = "Rosa está esperándote ((PL_ADVB='saassa')abajo). Estábamos ((PL_ADVB='la casa cuando comenzó el temporal')dentro). Los libros que buscas están ((PL_ADVB='en la estantería de allí arriba')arriba); Conociéndole, quizás ya tenga las cosas preparadas ((PL_ADVB='mente mesa principal, ademas la hemos arreglado')sobre)"


list_all_adverbs_of_place = ["adentro", "dentro", "arriba de", "arriba", "al medio", "abajo", "hacía", "hacia", "por sobre", "sobre las","sobre la", "sobre el", "sobre"]
place_reference = r"(?i:ws*)+"
pattern = re.compile(r"(((PL_ADVB='" + place_reference + r"')" + rf"({'|'.join(list_all_adverbs_of_place)})" + r"))", re.IGNORECASE)

print(re.findall(pattern, input_text))

input_text = re.sub(pattern, verify_need_to_restructure_where_capsule, input_text, re.IGNORECASE)

Advertisement

Answer

you can capture the specific part in the matched string using capture groups and then validate the string is present or not.

import re

input_text = "Rosa está esperándote ((PL_ADVB='saassa')abajo). Estábamos ((PL_ADVB='la casa cuando comenzó el temporal')dentro). Los libros que buscas están ((PL_ADVB='en la estantería de allí arriba')arriba); Conociéndole, quizás ya tenga las cosas preparadas ((PL_ADVB='mente mesa principal, ademas la hemos arreglado')sobre)"

list_all_adverbs_of_place = ["adentro", "dentro", "arriba de", "arriba", "al medio", "abajo", "hacía", "hacia",
                             "por sobre", "sobre las", "sobre la", "sobre el", "sobre"]

regex_pattern = r'((w+=W(?:w+W?s?)+)(w+))'
matches = []
data = re.finditer(regex_pattern, input_text)
for i in data:
    if i.group(1) in list_all_adverbs_of_place:
        matches.append(i.group())
print(matches)

>>> ["((PL_ADVB='saassa')abajo)", "((PL_ADVB='la casa cuando comenzó el temporal')dentro)", "((PL_ADVB='en la estantería de allí arriba')arriba)", "((PL_ADVB='mente mesa principal, ademas la hemos arreglado')sobre)"]
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement