Python 3有一個字符串方法叫做str.isidentifier
如何在Python 2.6中獲得Python isidentifer()功能?
我怎樣才能在Python 2.6中獲得類似的功能,重寫自己的正則表達式等?
Python 3有一個字符串方法叫做str.isidentifier
如何在Python 2.6中獲得Python isidentifer()功能?
我怎樣才能在Python 2.6中獲得類似的功能,重寫自己的正則表達式等?
re.match(r'[a-z_]\w*$', s, re.I)
應該做得很好。據我所知,沒有任何內置的方法。
在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
'[a-z_]'第一個字符。 – bobince 2010-03-30 12:34:11
好,但標識符可以以下劃線開頭。 – 2010-03-30 12:34:47
是的,當我寫這個時,我正在考慮這個問題。 – 2010-03-30 13:01:04
的令牌化模塊定義了一個名爲名稱
import re, tokenize, keyword
re.match(tokenize.Name + '$', somestr) and not keyword.iskeyword(somestr)
爲了達到這些目的,您需要'^'+ tokenize.Name +'$'。 – 2010-03-30 12:43:04
添加檢查python保留字,我喜歡這個。 – 2010-03-30 13:08:21
@Douglas S.J. De Couto - 您可以使用'import keyword','keyword.iskeyword(astring)'來檢查一個字符串是否爲關鍵字,請參閱它的文檔[here](http://docs.python.org/library/keyword.html #keyword.iskeyword)。 – Abbafei 2011-03-13 04:31:29
好的答案至今正則表達式。我會這樣寫。
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-30 13:06:15
@Douglas:這就是'我'的標誌。 – SilentGhost 2010-03-30 13:10:15
我用的是什麼:
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
're.match'匹配從行的開頭。 – SilentGhost 2010-03-30 13:17:16
我決定採取另一種破解這個,因爲已經有一些很好的建議。我會盡力鞏固他們。以下內容可以保存爲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()
所有的解決方案提出至今不支持Unicode或允許第一炭一個號碼,如果關於Python 3運行。
編輯:建議的解決方案應該只在Python 2上使用,在Python3上應該使用isidentifier
。這裏是一個應該在任何地方工作的解決方案:
re.match(r'^\w+$', name, re.UNICODE) and not name[0].isdigit()
基本上,它測試的東西能否由(至少1)個字符(包括數字),然後它會檢查的第一個字符是不是數字。
請注意,這不適用於unicode符號,例如, ''éllo'' – Almar 2016-03-06 10:10:12