时间:2022-08-12 10:02:31 | 栏目:Python代码 | 点击:次
import re print(re.match('飞兔小哥', '飞兔小哥教你零基础学编程')) print(re.match('学编程', '飞兔小哥教你零基础学编程'))
import re print(re.fullmatch('飞兔小哥教你零基础学编程', '飞兔小哥教你零基础学编程')) print(re.fullmatch('飞兔小哥', '飞兔小哥教你零基础学编程'))
import re print(re.search('autofelix', '飞兔小哥教你零基础学编程')) print(re.search('飞兔小哥', '飞兔小哥教你零基础学编程'))
import re # 去掉电话号码中的- num = re.sub(r'\D', '', '188-1926-8053') print(num) # 18819268053
import re # 去掉电话号码中的- num = re.subn(r'\D', '', '188-1926-8053') print(num) # (18819268053, 2)
import re print(re.split('a*', 'hello world')) # ['', 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', ''] print(re.split('a*', 'hello world', 1)) # ['', 'hello world']
import re pattern = re.compile(r'\W+') result1 = pattern.findall('hello world!') result2 = pattern.findall('hello world!', 0, 7) print(result1) # [' ', '!'] print(result2) # [' ']
import re pattern = re.compile(r'\W+') result = pattern.finditer('hello world!') for r in result: print(r)
import re pattern = re.compile(r'\W+')
import re content = "Cats are smarter than dogs" print(re.search(r'DOGS', content, re.M | re.I))