博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python正则表达式入门二
阅读量:4292 次
发布时间:2019-05-27

本文共 1281 字,大约阅读时间需要 4 分钟。

  • 至少出现一次。 和*号的区别在于* 可以出现零次。
+

示例

line = "tpyyyyyyypbpr123"regex_str = ".*?(p.+?p).*"match_result = re.match(regex_str, line)if match_result:    print(match_result.group(1))

运行结果

pyyyyyyyp

如果不加非贪婪问号的话就会出现pbp。

line = "tpyyyyyyypbpr123"regex_str = ".*(p.+p).*"match_result = re.match(regex_str, line)if match_result:    print(match_result.group(1))
pbp

如果在前面加一个问号

line = "tpyyyyyyypbpr123"regex_str = ".*?(p.+p).*"match_result = re.match(regex_str, line)if match_result:    print(match_result.group(1))

会出现

pyyyyyyypbp
  • 指定出现几次
{1} 指定 出现1次{2} 指定出现2次,以此类推{2,} 出现两次及以上。{2,5} 出现两到5次

示例

line = "tpyyyyyyypppbbpr123"regex_str = ".*(p.{1}p).*"match_result = re.match(regex_str, line)if match_result:    print(match_result.group(1))

结果

ppp

示例

line = "tpyyyyyyypppbbpr123"regex_str = ".*(py{2,7}p).*"match_result = re.match(regex_str, line)if match_result:    print(match_result.group(1))

结果

pyyyyyyyp
  • 任意出现几次字符
[]

示例

line = "atpr123btpr"regex_str = ".*?([abcd]tpr)"match_result = re.match(regex_str, line)if match_result:    print(match_result.group(1))

结果

atpr
  • 空格
此处注意是小s\s
  • 不为空格
此处注意为大S,且只能有一个字符\S 如果要表示 多个字符\S+
  • 表示任意汉字
[\u4e00-\u9FA5]

示例

line = "study in 南开大学"regex_str = ".*?([\u4e00-\u9FA5]+大学)"match_result = re.match(regex_str, line)if match_result:    print(match_result.group(1))

结果

我在南开大学

转载地址:http://vdkws.baihongyu.com/

你可能感兴趣的文章
常浏览的博客和网站
查看>>
Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.
查看>>
点击button实现Storyboard中TabBar Controller的tab切换
查看>>
Xcode 的正确打开方式——Debugging
查看>>
打包app出现的一个问题
查看>>
iOS在Xcode6中怎么创建OC category文件
查看>>
Expanding User-Defined Runtime Attributes in Xcode with Objective-C
查看>>
iOS7 UITabBar自定义选中图片显示为默认蓝色的Bug
查看>>
提升UITableView性能-复杂页面的优化
查看>>
25 iOS App Performance Tips & Tricks
查看>>
那些好用的iOS开发工具
查看>>
iOS最佳实践
查看>>
使用CFStringTransform将汉字转换为拼音
查看>>
更轻量的 View Controllers
查看>>
Chisel-LLDB命令插件,让调试更Easy
查看>>
时间格式化hh:mm:ss和HH:mm:ss区别
查看>>
When to use Delegation, Notification, or Observation in iOS
查看>>
Objective-C Autorelease Pool 的实现原理
查看>>
编程语言大牛王垠:编程的智慧,带你少走弯路
查看>>
ios指令集以及基于指令集的app包压缩策略
查看>>