mercredi 4 mars 2020

Check is regex result is 'None' for conditional 'If' statement?

Trying to skip an action when the regex result returns as 'None':

    for idx, val in enumerate(S):
        num = re.search('(?<=Number\': )[^,]+', val)
        name = re.search('(?<=Name\': )[^,]+', val)

        if num and name:
            S_C.append(['S' + num.group(0).strip('\''), name.group(0).strip('\'')])
            print('yes')

The regex result for my code does return some non-None values but the If num and name: doesn't seem to work. Neither does If num is not None which I have tried.

Aucun commentaire:

Enregistrer un commentaire