正则表达式本身是一种小型的、高度专业化的编程语言,下面这篇文章主要给大家介绍了关于python利用正则表达式实现搜索单词的相关资料,文中给出了详细的示例代码,需要的朋友可以参考借鉴,下面来一起看看吧。
前言
在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配。正则表达式模式被编译成一系列的字节码,然后由用C编写的匹配引擎执行。
比如下面的例子,就是用来从一段文字里查找一个单词,如下:
示例代码
立即学习“Python免费学习笔记(深入)”;
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 "{}"\nin "{}"\nfrom {} 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()表示结束位置。
总结











