我想从字符串中提取某种变量名称(不允许使用数字、_等),不使用正则表达式并且尽可能短(即使它不可读)。
我该怎么做?
示例:
“asd123*456qwe_h”-> [asd,qwe,h]。
(不允许使用“a”、“qw”等)
有很多方法可以在不使用 RE 的情况下实现这一目标。这是一个:
from string import punctuation, digits
text = "asd123*456qwe_h"
ignore = set(punctuation + digits)
result = ['']
for c in text:
if c in ignore:
if result[-1]:
result.append('')
else:
result[-1] = result[-1] + c
print(result)
输出:
['asd', 'qwe', 'h']