python里使用正则表达式的search()函数实现指定位置搜索

来源:互联网 发布:js按钮current trigger 编辑:程序博客网 时间:2024/05/21 18:41
前面学习过search()可以从任意一个文本里搜索匹配的字符串,也就是说可以从任何位置里搜索到匹配的字符串。但是现实世界很复杂多变的,比如限定你只能从第100个字符的位置开始匹配,100个字符之前的不要匹配,这样的需求怎么样实现呢?来看下面的例子,它就是指定位置开始搜索:

#python 3.6#蔡军生 #http://blog.csdn.net/caimouse/article/details/51749579#import retext = 'This is some text -- with punctuation.'pattern = re.compile(r'\b\w*is\w*\b')print('Text:', text)print()pos = 0while True:    match = pattern.search(text, pos)    if not match:        break    s = match.start()    e = match.end()    print('  {:>2d} : {:>2d} = "{}"'.format(        s, e - 1, text[s:e]))    # Move forward in text for the next search    pos = e


结果输出如下:

Text: This is some text -- with punctuation.


   0 :  3 = "This"
   5 :  6 = "is"

在这个例子里,实现一个低效的iterall()函数相同的功能。

深入浅出Numpy
http://edu.csdn.net/course/detail/6149 

Python游戏开发入门

http://edu.csdn.net/course/detail/5690

你也能动手修改C编译器

http://edu.csdn.net/course/detail/5582

纸牌游戏开发

http://edu.csdn.net/course/detail/5538 

五子棋游戏开发

http://edu.csdn.net/course/detail/5487
RPG游戏从入门到精通
http://edu.csdn.net/course/detail/5246
WiX安装工具的使用
http://edu.csdn.net/course/detail/5207
俄罗斯方块游戏开发
http://edu.csdn.net/course/detail/5110
boost库入门基础
http://edu.csdn.net/course/detail/5029
Arduino入门基础
http://edu.csdn.net/course/detail/4931
Unity5.x游戏基础入门
http://edu.csdn.net/course/detail/4810
TensorFlow API攻略
http://edu.csdn.net/course/detail/4495
TensorFlow入门基本教程
http://edu.csdn.net/course/detail/4369
C++标准模板库从入门到精通 
http://edu.csdn.net/course/detail/3324
跟老菜鸟学C++
http://edu.csdn.net/course/detail/2901
跟老菜鸟学python
http://edu.csdn.net/course/detail/2592
在VC2015里学会使用tinyxml库
http://edu.csdn.net/course/detail/2590
在Windows下SVN的版本管理与实战 
http://edu.csdn.net/course/detail/2579
Visual Studio 2015开发C++程序的基本使用 
http://edu.csdn.net/course/detail/2570
在VC2015里使用protobuf协议
http://edu.csdn.net/course/detail/2582
在VC2015里学会使用MySQL数据库
http://edu.csdn.net/course/detail/2672


阅读全文
2 0
原创粉丝点击