前言
在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配。正则表达式模式被编译成一系列的字节码,然后由用C编写的匹配引擎执行。
比如下面的例子,就是用来从一段文字里查找一个单词,如下:
示例代码
import re pattern = 'this' text = 'http://blog.csdn.net/caimouse is great, this is great way!' match = re.search(pattern, text) s = match.start() e = match.end() print('Found "{}" in "{}" from {} to {} ("{}")'.format( match.re.pattern, match.string, s, e, text[s:e]))
结果输出如下:
Found "this" in "http://blog.csdn.net/caimouse is great, this is great way!" from 40 to 44 ("this")
在这里使用start()
表示匹配的开始位置,end()表示结束位置。
总结
Copyright © 2019- ovod.cn 版权所有 湘ICP备2023023988号-4
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务