如何提取最大可能的字符串的 alpha 子串

问题描述 投票:0回答:1

我想从字符串中提取某种变量名称(不允许使用数字、_等),不使用正则表达式并且尽可能短(即使它不可读)。

我该怎么做?

示例:

“asd123*456qwe_h”-> [asd,qwe,h]。

(不允许使用“a”、“qw”等)

python substring
1个回答
0
投票

有很多方法可以在不使用 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']
© www.soinside.com 2019 - 2024. All rights reserved.