Walrus operator for filtering regex searches in list comprehension

Tags: , , ,



I have a Python list of strings. I want to do the regex search on each element, filtering only those elements where I managed to capture the regex group. I think I can do the regex search only once using the walrus operator from Python 3.8. So far I have:

attacks = [match
           for attack in attacks
           if (match := re.search(r"(([0-9]+d[sS]+))", attack).group() is not None)]

The logic is: I take the found group if the regex search returned anything, which means it is not None. The problem is, the bevahiour is weird – I can print() before this list comprehension, the program finishes with code 0, but there is no result and print() after the list comprehension does not work. What am I doing wrong?

EDIT:

Full code:

text = "Speed 30 ft. Melee short sword +3 (1d6+1/19-20) Ranged light crossbow +3 (1d8/19-20)  Special Attacks sneak attack +1d6 Spell-Like Abilities (CL 1st) "
if attacks:
    attacks = attacks.group().split(")")
    attacks = [match
               for attack in attacks
               if (match := re.search(r"(([0-9]+d[sS]+))", attack).group() is not None)]

Answer

Remove the .group() is not None. If there isn’t any match, re.search() returns None and exception is thrown:

import re


attacks = ['(7dW)', 'xxx']
attacks = [match
           for attack in attacks
           if (match := re.search(r"(([0-9]+d[sS]+))", attack))]

print(attacks)

Prints:

[<re.Match object; span=(0, 5), match='(7dW)'>]


Source: stackoverflow