Python match unicode string as unicode

Viewed 55

I am trying to match a unicode string, such that the unicode will not match the string literal.

def validate(username):
    if "admin" in username:
        return False
    else:
        return True

validate(username)

If I pass username="\u0061\u0064\u006d\u0069\u006e", it will return False, since it is converting the unicode, and then matching, and "\u0061\u0064\u006d\u0069\u006e" is unicode for admin. Is there a way to match before converting? The input is not converted, it starts off as unicode. I have tried using regex, but have not succeeded.

1 Answers

In Python 3, there's no longer a distinction between "unicode" and "string". So the string "\u0061\u0064\u006d\u0069\u006e" is just a string of the characters a, d, m, i, n, but using unicode codepoint escape sequences; there's no "conversion" going on here, it is exactly equivalent to entering "admin".

What are you trying to achieve?

Remember that string escape sequences, like \u0061, are translated during parsing by Python, they never actually end up as part of the string. If, instead, a user, for example, enters the literal string of characters \u0061\u0064\u006d\u0069\u006e into a text form, what you will get, in Python notation, will be a string equivalent to "\\u0061\\u0064\\u006d\\u0069\\u006e" (notice the escaped backslashes, to indicate these are literal backslashes and not escape sequences).

Related