Python 3:如何检查字符串是否可以是有效变量



我有一个字符串,想检查它是否可以用作有效变量而不会出现语法错误。例如

def variableName(string):
    #if string is valid variable name:
        #return True
    #else:
        #return False
input >>> variableName("validVariable")
output >>> True
input >>> variableName("992variable")
output >>> False

我不想使用 .isidentifier((。我想做一个我自己的功能。

以下答案仅适用于"旧式"Python-2.7 标识符;

"validVariable".isidentifier()
#True
"992variable".isidentifier()
#False

由于您在我发布答案后更改了问题,请考虑编写一个正则表达式:

re.match(r"[_a-z]w*$", yourstring,flags=re.I)
在 Python 3 中,

有效的标识符可以包含 ASCII 范围之外的字符,因为您不想使用 str.isidentifier ,您可以在 Python 中编写自己的版本。

它的规格可以在这里找到:https://www.python.org/dev/peps/pep-3131/#specification-of-language-changes

实现:

import keyword
import re
import unicodedata

def is_other_id_start(char):
    """
    Item belongs to Other_ID_Start in
    http://unicode.org/Public/UNIDATA/PropList.txt
    """
    return bool(re.match(r'[u1885-u1886u2118u212Eu309B-u309C]', char))

def is_other_id_continue(char):
    """
    Item belongs to Other_ID_Continue in
    http://unicode.org/Public/UNIDATA/PropList.txt
    """
    return bool(re.match(r'[u00B7u0387u1369-u1371u19DA]', char))

def is_xid_start(char):
    # ID_Start is defined as all characters having one of
    # the general categories uppercase letters(Lu), lowercase
    # letters(Ll), titlecase letters(Lt), modifier letters(Lm),
    # other letters(Lo), letter numbers(Nl), the underscore, and
    # characters carrying the Other_ID_Start property. XID_Start
    # then closes this set under normalization, by removing all
    # characters whose NFKC normalization is not of the form
    # ID_Start ID_Continue * anymore.
    category = unicodedata.category(char)
    return (
        category in {'Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl'} or
        is_other_id_start(char)
    )

def is_xid_continue(char):
    # ID_Continue is defined as all characters in ID_Start, plus
    # nonspacing marks (Mn), spacing combining marks (Mc), decimal
    # number (Nd), connector punctuations (Pc), and characters
    # carryig the Other_ID_Continue property. Again, XID_Continue
    # closes this set under NFKC-normalization; it also adds U+00B7
    # to support Catalan.
    category = unicodedata.category(char)
    return (
        is_xid_start(char) or
        category in {'Mn', 'Mc', 'Nd', 'Pc'} or
        is_other_id_continue(char)
    )

def is_valid_identifier(name):
    # All identifiers are converted into the normal form NFKC
    # while parsing; comparison of identifiers is based on NFKC.
    name = unicodedata.normalize(
        'NFKC', name
    )
    # check if it's a keyword
    if keyword.iskeyword(name):
        return False
    # The identifier syntax is <XID_Start> <XID_Continue>*.
    if not (is_xid_start(name[0]) or name[0] == '_'):
        return False
    return all(is_xid_continue(char) for char in name[1:])
if __name__ == '__main__':
    # From goo.gl/pvpYg6
    assert is_valid_identifier("a") is True
    assert is_valid_identifier("Z") is True
    assert is_valid_identifier("_") is True
    assert is_valid_identifier("b0") is True
    assert is_valid_identifier("bc") is True
    assert is_valid_identifier("b_") is True
    assert is_valid_identifier("µ") is True
    assert is_valid_identifier("𝔘𝔫𝔦𝔠𝔬𝔡𝔢") is True
    assert is_valid_identifier(" ") is False
    assert is_valid_identifier("[") is False
    assert is_valid_identifier("©") is False
    assert is_valid_identifier("0") is False
<小时 />

你可以在这里和这里分别查看CPython和Pypy的植入。

您可以使用正则表达式。

例如:

isValidIdentifier = re.match("[A-Za-z_](0-9A-Za-z_)*",identifier)

请注意,他只检查字母数字字符。 实际标准支持其他字符。看这里: https://www.python.org/dev/peps/pep-3131/

您可能还需要排除保留字,例如 def、True、False、... 看这里: https://www.programiz.com/python-programming/keywords-identifier

最新更新