samedi 27 mai 2017

Why condition returns True using regular expressions for finding special characters in the string?

I need to validate the variable names:

name = ["2w2", " variable", "variable0", "va[riable0", "var_1__Int", "a", "qq-q"]

And just names "variable0", "var_1__Int" and "a" are correct.

I could Identify most of "wrong" name of variables using regex:

import re
if re.match("^\d|\W|.*-|[()[]{}]", name):
    print(False)
else:
    print(True)

However, I still become True result for va[riable0. Why is it the case? I control for all type of parentheses.

Aucun commentaire:

Enregistrer un commentaire