嗨我正在从文件中读取一行,我需要获取最长的标识符,例如在行中
MY_Variable = Some_Variable+New_Variable;
在上面这行我需要得到“Some_Variable”。我试过下面的代码,但我得到的结果是
Some_Variable+New_Variable
我试过这个
if(re.search('[a-zA-Z]', Line_Read)): ## To check whether line has identifier or not
if(len(max(Line_Read.split(), key=len))>32): ## length of the longest string is greater than 32 or not?
print max(Line_Read.split(), key=len) ## printing the identifier
请帮忙。谢谢
答案 0 :(得分:4)
Python标识符可以定义为[a-zA-Z_]\w*
:
max(re.findall(r'[a-zA-Z_]\w*', Line_Read), key=len)
返回
'Some_Variable'