2 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超7個(gè)贊
import re
match = re.search('^[\d]+', '1234h')
if match:
print(match.group(0))
對(duì)于“1234”和“1234h” ,它將返回“1234 ”。它在開(kāi)始后找到一系列數(shù)字,并在字母后忽略。

TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊
list = ['1234abc' , '5278', 'abc58586']
def range_sanitizer(s, lower_bound, upper_bound):
return ''.join([x for x in s if lower_bound < x < upper_bound])
def remove_non_digits(s):
return range_sanitizer(s, '0', '9')
def sanitize_list(list, sanitizer=remove_non_digits):
return [sanitizer(item) for item in list]
if '__main__' == __name__:
sanitized_list=sanitize_list(list)
# ['1234', '5278', '58586']
print(sanitized_list)
添加回答
舉報(bào)