import re
import numpy as np
# 邮箱验证# 写一个正则表达式,用于验证一个字符串是否符合常见的邮箱格式。
email1 ="example@example.com"if re.match(r'\w+@\w+.\w+', email1):print(True)# URL提取# 给定一个包含多个URL的字符串,使用正则表达式提取所有的URL。
text ="访问网站https://www.example.com获取更多信息,或者点击http://another.site"
result = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', text)print(result)# 电话号码提取# 从一个文本文件中读取内容,使用正则表达式提取所有的电话号码。
text ="我的电话是123-4567-8901,你可以随时联系我。"
result = re.findall(r'\d{3}-\d{4}-\d{4}', text)print(result)# HTML标签去除# 给定一个包含HTML标签的字符串,使用正则表达式去除所有的HTML标签,只保留文本内容。
html_text ="<p>这是一段文本。</p><span>更多内容在这里。</span>"
result = re.sub(r'<[/]?\w+>','', html_text)print(result)# 日期格式验证# 编写一个正则表达式,用于验证一个字符串是否符合“YYYY-MM-DD”的日期格式。
date ="2023-03-15"# 重复字符检测# 用python 编写一个正则表达式,用于检测一个字符串中是否有连续重复出现三次或以上的字符。
text ="aaaabbbccddeee"
pattern =r'(.)\11'# result = re.search(pattern, text)# print(result)# IP地址验证# 编写一个正则表达式,用于验证一个字符串是否符合IPv4地址的格式。
ip_address ="192.168.1.1"# 数字提取# 从一个包含数字和字母的字符串中提取所有的数字。
text ="我有10个苹果和5个橙子。"
result = re.findall(r'\d+', text)print(result)# 单词边界匹配# 使用正则表达式匹配一个字符串中所有以“py”开头的单词。
text ="python is fun, pycharm is a great IDE"print(re.findall(r'py\w*', text))# 替换特定模式# 使用正则表达式将字符串中所有的数字替换为“*”。
text ="我有1个苹果,2个橙子和3个香蕉。"print(re.sub(r'\d','*', text))
a =("我","爱","中","国")
b =("我","爱","中","国")
C =["我","爱","中","国"]print(''.join(a)+'-'.join(b)+'*'.join(C))
b ='我爱中国我-爱-中-国我*爱*中*国'
result = b.replace('爱','ai')print(result)