鱼C论坛

 找回密码
 立即注册
查看: 4366|回复: 12

[技术交流] 正则表达式

[复制链接]
发表于 2021-6-26 20:43:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
一.使用search()方法匹配字符串(1)
  1. import re
  2. pattern = 'mr_\w+'                       # 模式字符串
  3. string = 'MR_SHOP mr_shop'              # 要匹配的字符串
  4. match = re.search(pattern,string,re.I)  # 搜索字符串,不区分大小写
  5. print(match)                              # 输出匹配结果
  6. string = '项目名称MR_SHOP mr_shop'
  7. match = re.search(pattern,string,re.I)  # 搜索字符串,不区分大小写
  8. print(match)                              # 输出匹配结果
复制代码


一.使用search()方法匹配字符串(2)
  1. import re                        # 导入re模块
  2. # 表达式,(\d?)+表示多个数字可有可无,\s空格可有可无,([\u4e00-\u9fa5]?)+多个汉字可有可无
  3. pattern = '(\d?)+mrsoft\s?([\u4e00-\u9fa5]?)+'
  4. match = re.search(pattern,'01mrsoft')  # 匹配字符串,mrsoft前有01数字,匹配成功
  5. print(match)                            # 打印匹配结果
  6. match = re.search(pattern,'mrsoft')  # 匹配字符串,mrsoft匹配成功
  7. print(match)                            # 打印匹配结果
  8. match = re.search(pattern,'mrsoft ')  # 匹配字符串,mrsoft后面有一个空格,匹配成功
  9. print(match)                            # 打印匹配结果
  10. match = re.search(pattern,'mrsoft 第一')  # 匹配字符串,mrsoft后面有空格和汉字,匹配成功
  11. print(match)                            # 打印匹配结果
  12. match = re.search(pattern,'rsoft 第一')  # 匹配字符串,rsoft后面有空格和汉字,匹配失败
  13. print(match)                            # 打印匹配结果
复制代码


一.使用search()方法匹配字符串(3)
  1. import re                        # 导入re模块
  2. pattern = r'\bmr\b'               # 表达式,mr两侧均有边界
  3. match = re.search(pattern,'mrsoft')  # 匹配字符串,mr右侧不是边界是soft,匹配失败
  4. print(match)                            # 打印匹配结果
  5. match = re.search(pattern,'mr soft')  # 匹配字符串,mr左侧为边界右侧为空格,匹配成功
  6. print(match)                            # 打印匹配结果
  7. match = re.search(pattern,' mrsoft ')  # 匹配字符串,mr左侧为空格右侧为soft空格,匹配失败
  8. print(match)                            # 打印匹配结果
  9. match = re.search(pattern,'mr.soft')  # 匹配字符串,mr左侧为边界右侧为“.”,匹配成功
  10. print(match)                            # 打印匹配结果  
复制代码


二.使用findall()方法匹配字符串(1)
  1. import re
  2. pattern = 'mr_\w+'                        # 模式字符串
  3. string = 'MR_SHOP mr_shop'                # 要匹配的字符串
  4. match = re.findall(pattern,string,re.I)  # 搜索字符串,不区分大小写
  5. print(match)                                # 输出匹配结果
  6. string = '项目名称MR_SHOP mr_shop'
  7. match = re.findall(pattern,string)        # 搜索字符串,区分大小写
  8. print(match)                                # 输出匹配结果
复制代码


二.使用findall()方法匹配字符串(2)
  1. import re                        # 导入re模块
  2. pattern = 'https://.*/'               # 表达式,“.*”获取www.hao123.com
  3. match = re.findall(pattern,'https://www.hao123.com/')  # 匹配字符串
  4. print(match)                            # 打印匹配结果


  5. import re                        # 导入re模块
  6. pattern = 'https://(.*)/'               # 表达式,“.*”获取www.hao123.com
  7. match = re.findall(pattern,'https://www.hao123.com/')  # 匹配字符串
  8. print(match)                            # 打印匹配结果
复制代码


二.使用findall()方法匹配字符串(3)
  1. import re                        # 导入re模块
  2. pattern = 'https://.*(\d+).com/'               # 表达式,“.*”获取www.hao123.com
  3. match = re.findall(pattern,'https://www.hao123.com/')  # 匹配字符串
  4. print(match)                            # 打印匹配结果

  5. import re                        # 导入re模块
  6. pattern = 'https://.*?(\d+).com/'               # 表达式,“.*”获取www.hao123.com
  7. match = re.findall(pattern,'https://www.hao123.com/')  # 匹配字符串
  8. print(match)                            # 打印匹配结果


  9. import re                        # 导入re模块
  10. pattern = 'https://(.*?)'               # 表达式,“.*?”获取www.hao123.com/
  11. match = re.findall(pattern,'https://www.hao123.com/')  # 匹配字符串
  12. print(match)                            # 打印匹配结果
  13. pattern = 'https://(.*)'               # 表达式,“.*”获取www.hao123.com/
  14. match = re.findall(pattern,'https://www.hao123.com/')  # 匹配字符串
  15. print(match)                            # 打印匹配结果
复制代码


三.字符串处理  sub() 替换字符串
  1. import re
  2. pattern = r'1[34578]\d{9}'                       # 定义要替换的模式字符串
  3. string = '中奖号码为:84978981 联系电话为:13611111111'
  4. result = re.sub(pattern,'1XXXXXXXXXX',string)  # 替换字符串
  5. print(result)


  6. import re                        # 导入re模块
  7. string = 'hk400 jhkj6h7k5 jhkjhk1j0k66'     # 需要匹配的字符串
  8. pattern = '[a-z]'                           # 表达式
  9. match = re.sub(pattern,'',string,flags=re.I)  # 匹配字符串,将所有字母替换为空,并区分大小写
  10. print(match)                                # 打印匹配结果


  11. import re                        # 导入re模块
  12. # 需要匹配的字符串
  13. string = 'John,I like you to meet Mr. Wang,Mr. Wang, this is our Sales Manager John. John, this is Mr. Wang.'
  14. pattern = 'Wang'      # 表达式
  15. match = re.subn(pattern,'Li',string)  # 匹配字符串,将所有Wang替换为Li,并统计替换次数
  16. print(match)                                # 打印匹配结果
  17. print(match[1])                             # 打印匹配次数
复制代码


三.字符串处理  split() 分割字符串
  1. import re
  2. pattern = r'[?|&]'                  # 定义分割符
  3. url = 'http://www.mingrisoft.com/login.jsp?username="mr"&pwd="mrsoft"'
  4. result = re.split(pattern,url)     # 分割字符串
  5. print(result)

  6. import re                        # 导入re模块
  7. # 需要匹配的字符串
  8. string = '预定|K7577|CCT|THL|CCT|LYL|14:47|16:51|02:04|Y|'
  9. pattern = '\|'      # 表达式
  10. match = re.split(pattern,string,maxsplit=1)  # 匹配字符串,通过第一次出现的|进行分割
  11. print(match)                                # 打印匹配结果
复制代码

评分

参与人数 1鱼币 +5 收起 理由
贪睡的苏简 + 5 加油

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-26 21:51:55 | 显示全部楼层

回帖奖励 +1 鱼币

不错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-26 23:54:56 | 显示全部楼层

回帖奖励 +1 鱼币

感谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-27 00:08:17 | 显示全部楼层

回帖奖励 +1 鱼币

   领币  感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-6-27 01:19:55 | 显示全部楼层

回帖奖励 +1 鱼币

厉害
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-27 06:26:11 | 显示全部楼层

回帖奖励 +1 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-27 18:20:23 | 显示全部楼层

回帖奖励 +1 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-27 18:35:44 | 显示全部楼层

回帖奖励 +1 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-28 09:04:45 | 显示全部楼层

回帖奖励 +1 鱼币

厉害
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-29 14:09:31 | 显示全部楼层

回帖奖励 +1 鱼币

牛逼
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-29 14:16:48 | 显示全部楼层

回帖奖励 +1 鱼币

感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-15 20:11:12 | 显示全部楼层
+3,感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-8 23:27:04 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 01:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表