Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > python入门

python利用正则表达式搜索单词示例代码

来源:中文源码网    浏览:150 次    日期:2024-05-09 09:46:41
【下载文档:  python利用正则表达式搜索单词示例代码.txt 】


python利用正则表达式搜索单词示例代码
前言
在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 "{}"\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()表示结束位置。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对中文源码网的支持。

相关内容