python–正则
正则表达式
正则表达式:导入re模块 参考链接: link.
import re
'''正则表达式模式'''
'''正则表达式修饰符'''
a = '1234567890zabbbbcdefg*25.254.1.12!.abcd'
print(re.match('123', a))
match_ans = re.match('123', a)
print(match_ans.group())
print(re.search(r'.', a))
print(re.search(r'\.', a))
print(re.search(r'\d', a))
print(re.search(r'[aeiou]', a))
print(re.search(r'[b-z]', a))
print(re.search(r'[0-9][4-8][6-7]', a))
print(re.search(r'ab{4}c', a))
print(re.search(r'ab{3,5}c', a))
print(re.search(r'(([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])\.){3}([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])', a))
print(re.search((r'(abc)\1'), 'abcabcd'))
ano_a = re.sub(r'1', '(1)', a)
print(ano_a)
a_re = re.compile(r'([0-9]+)([a-z]+)')
m = a_re.match(a)
print(m)
print(type(m), m.start(), m.end(), m.span())
print(m.group(), m.group(1), m.group(2), m.groups())
print(re.findall(r'[a-z]+', a))
print(re.split(r'[a-z]+', a))
print(re.split('\W+', a))
print(re.split('(\W+)', a))
print(re.split('\W+', a, 1))
|