import re
def had_number(sectence):
result = re.findall('\d', sectence)
print(result)
for i in result:
return True
return False
def remove_number(sectence):
result = re.findall('\D', sectence)
print(result)
return ''.join(result)
def startswith(sub, sectence):
_sub = '\A%s' % sub
result = re.findall(_sub, sectence)
for i in result:
return True
return False
def endswith(sub, sectence):
_sub = '%s\Z' % sub
print(_sub)
result = re.findall(_sub, sectence)
print(result)
if len(result) != 0:
return True
else:
return False
def real_len(sectence):
result = re.findall('\S', sectence)
print(result)
return len(result)
if __name__ == '__main__':
sectence = 'i am muller, i am 34'
result = had_number(sectence)
print(result)
result = remove_number(sectence)
print(result)
sectence = 'hello messi, i am muller. i am 34 year\'s old'
print(re.findall('\W', sectence))
result = startswith('hell', sectence)
print(result)
result = endswith('olds', sectence)
print(result)
print(len(sectence))
result = real_len(sectence)
print(result)
----------------------------------
输出结果如下:
['3', '4']
True
['i', ' ', 'a', 'm', ' ', 'm', 'u', 'l', 'l', 'e', 'r', ',', ' ', 'i', ' ', 'a', 'm', ' ']
i am muller, i am
[' ', ',', ' ', ' ', ' ', '.', ' ', ' ', ' ', ' ', "'", ' ']
True
olds\Z
[]
False
44
['h', 'e', 'l', 'l', 'o', 'm', 'e', 's', 's', 'i', ',', 'i', 'a', 'm', 'm', 'u', 'l', 'l', 'e', 'r', '.', 'i', 'a', 'm', '3', '4', 'y', 'e', 'a', 'r', "'", 's', 'o', 'l', 'd']
35
评论