ValueError: too many values to unpack (expected 2) , when I try to extract only 2 substrings from a regex pattern

  python, python-3.x, regex, regex-group, string

This is the code but the part of the error is where is the extraction of the substrings after validating the regex pattern structure

def name_and_img_identificator(input_text, text):
    input_text = re.sub(r"([^nu0300-u036f]|n(?!u0303(?![u0300-u036f])))[u0300-u036f]+", r"", normalize("NFD", input_text), 0, re.I)
    input_text = normalize( 'NFC', input_text) # -> NFC
    input_text_to_check = input_text.lower() #Convierte a minuscula todo
    
    regex_patron_01 = r"s*¿?(?:dime los|dime las|dime unos|dime unas|dime|di|cuales son los|cuales son las|cuales son|cuales|que animes|que|top)s*((?:w+s*)+)s*(?:de series anime|de anime series|de animes|de anime|animes|anime)s*(?:similares al|similares a|similar al|similar a|parecidos al|parecidos a|parecido al|parecido a)s*(?:la serie de anime|series de anime|la serie anime|la serie|anime|)s*(llamada|conocida como|cuyo nombre es|la cual se llama|)s*((?:w+s*)+)s*??"

    m = re.search(regex_patron_01, input_text_to_check, re.IGNORECASE) #Con esto valido la regex haber si entra o no en el bloque de code

    if m:
        num, anime_name = m.groups()[2]

        num = num.strip()
        anime_name = anime_name.strip()
        print(num)
        print(anime_name)

    return text

input_text_str = input("ingrese: ")
text = ""

print(name_and_img_identificator(input_text_str, text))

It gives me this error, and the truth is I don’t know how to structure this regex pattern so that it only extracts those 2 values (substrings) from that input

Traceback (most recent call last):
  File "serie_recommendarion_for_chatbot.py", line 154, in <module>
    print(serie_and_img_identificator(input_text_str, text))
  File "anime_recommendarion_for_chatbot.py", line 142, in name_and_img_identificator
    num, anime_name = m.groups()
ValueError: too many values to unpack (expected 2)

If I put an input like this:
‘Dame el top 8 de animes parecidos a Gundam’

I need you to extract:

num = '8'
anime_name = 'Gundam'

How do I have to fix my regex sequence in that case?

Source: Python Questions

LEAVE A COMMENT