16

Python 3 には、という文字列メソッドがあります。str.isidentifier

自分の正規表現を書き直す以外に、Python 2.6 で同様の機能を得るにはどうすればよいですか?

4

8 に答える 8

15

tokenize モジュールは Name と呼ばれる正規表現を定義します

import re, tokenize, keyword
re.match(tokenize.Name + '$', somestr) and not keyword.iskeyword(somestr)
于 2010-03-30T12:37:44.480 に答える
2
re.match(r'[a-z_]\w*$', s, re.I)

うまくやるべきです。私の知る限り、組み込みメソッドはありません。

于 2010-03-30T12:18:41.180 に答える
1

いくつかの良い提案があったので、私はこれをもう一度試してみることにしました。私はそれらを統合しようとします。以下は Python モジュールとして保存し、コマンドラインから直接実行できます。実行すると、関数がテストされるため、正しいことが証明されます (少なくともドキュメントでその機能が示されている限り)。

import keyword
import re
import tokenize

def isidentifier(candidate):
    """
    Is the candidate string an identifier in Python 2.x
    Return true if candidate is an identifier.
    Return false if candidate is a string, but not an identifier.
    Raises TypeError when candidate is not a string.

    >>> isidentifier('foo')
    True

    >>> isidentifier('print')
    False

    >>> isidentifier('Print')
    True

    >>> isidentifier(u'Unicode_type_ok')
    True

    # unicode symbols are not allowed, though.
    >>> isidentifier(u'Unicode_content_\u00a9')
    False

    >>> isidentifier('not')
    False

    >>> isidentifier('re')
    True

    >>> isidentifier(object)
    Traceback (most recent call last):
    ...
    TypeError: expected string or buffer
    """
    # test if candidate is a keyword
    is_not_keyword = candidate not in keyword.kwlist
    # create a pattern based on tokenize.Name
    pattern_text = '^{tokenize.Name}$'.format(**globals())
    # compile the pattern
    pattern = re.compile(pattern_text)
    # test whether the pattern matches
    matches_pattern = bool(pattern.match(candidate))
    # return true only if the candidate is not a keyword and the pattern matches
    return is_not_keyword and matches_pattern

def test():
    import unittest
    import doctest
    suite = unittest.TestSuite()
    suite.addTest(doctest.DocTestSuite())
    runner = unittest.TextTestRunner()
    runner.run(suite)

if __name__ == '__main__':
    test()
于 2010-03-30T16:35:44.013 に答える
1

Python < 3.0 では、識別子に Unicode 文字を使用できないため、これは非常に簡単です。それは仕事をするはずです:

import re
import keyword

def isidentifier(s):
    if s in keyword.kwlist:
        return False
    return re.match(r'^[a-z_][a-z0-9_]*$', s, re.I) is not None
于 2010-03-30T12:22:00.143 に答える
1

これまでのところ良い答え。このように書きます。

import keyword
import re

def isidentifier(candidate):
    "Is the candidate string an identifier in Python 2.x"
    is_not_keyword = candidate not in keyword.kwlist
    pattern = re.compile(r'^[a-z_][a-z0-9_]*$', re.I)
    matches_pattern = bool(pattern.match(candidate))
    return is_not_keyword and matches_pattern
于 2010-03-30T12:37:54.293 に答える
0

これまでに提案されたすべてのソリューションは、Unicode をサポートしていないか、Python 3 で実行する場合、最初の文字に数字を許可していません。

編集: 提案されたソリューションは Python 2 でのみ使用し、Python3isidentifierで使用する必要があります。これは、どこでも機能するはずのソリューションです。

re.match(r'^\w+$', name, re.UNICODE) and not name[0].isdigit()

基本的に、何かが(少なくとも1つの)文字(数字を含む)で構成されているかどうかをテストし、最初の文字が数字ではないことを確認します。

于 2016-03-06T10:13:04.840 に答える
0

私が使用しているもの:

def is_valid_keyword_arg(k):
    """
    Return True if the string k can be used as the name of a valid
    Python keyword argument, otherwise return False.
    """
    # Don't allow python reserved words as arg names
    if k in keyword.kwlist:
        return False
    return re.match('^' + tokenize.Name + '$', k) is not None
于 2010-03-30T13:12:51.197 に答える