import re
prog = re.compile(r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+')
text ='你好123@163.com'
text2 ='xiaoming@sf.com再见'
text3 ='123456789@qq.com哈哈'
text4 ='123456789@QQ.com哦哦'for i in[text,text2,text3,text4]:
res = prog.findall(i)if res:print(res)else:print('无匹配')
6、匹配车牌
import re
car_search =r'[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁台琼使领军北南成广沈济空海]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂领学警港澳]{1}(?!\d)'
text ="24年3月运费 起运地:上海 目的地:全国 车种车号:厢式冷藏车浙沪DQ4557 货物信息:食品"# 需要抽取的文本defcar_ID_extract(text):
all_car_id = re.findall(car_search, text)
car_id =[]
car_id1 =""if all_car_id:for i in all_car_id:ifnot i in car_id:
car_id.append(i)for i in car_id:
car_id1 = car_id1 +' '+"".join(tuple(i))# 将列表转字符串return car_id1.strip()# 返回字符串print(car_ID_extract(text))
7、xx为开头yy为结尾
下面的代码是:匹配
以【起运地 或 出发地 或 发出地】为开头 以【目的地 或 到达地 】为结尾,中间的字符串
import re
defstart(text):
pattern =r"(?:起运地|出发地|发出地)(.*?)(?=目的地|到达地)"match= re.search(pattern, text)ifmatch:
result =match.group()# print("匹配到的内容:", result)return result
else:# print("没有找到匹配的内容。")return':'
res = start("出发地:由上海出发到达江苏淮安涟水中专技术学院-目的地:")print(res)
9、匹配中文
import re
text ="舟山96L*4"
chinese_characters = re.findall(r'[\u4e00-\u9fff]+', text)print(chinese_characters)
10、匹配非中文
import re
defremove_chinese(text):
pattern = re.compile(r'[\u4e00-\u9fa5]')return re.sub(pattern,'', text)print(remove_chinese('哈哈哈123-145苹果'))