python利用正则表达式搜索单词示例代码
作者:caimouse 时间:2023-02-21 11:31:03
前言
在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()表示结束位置。
来源:http://blog.csdn.net/caimouse/article/details/78069953
标签:python,正则表达式,搜索单词
0
投稿
猜你喜欢
下载Internet Explorer 9 平台预览版4
2010-08-05 20:59:00
什么是XSLT,什么是XPath
2008-01-21 13:12:00
php中instanceof 与 is_a()区别分析
2023-11-19 06:04:56
再谈 Web 默认字体
2009-11-24 12:48:00
DHTML 打造 Picture Spelling
2013-08-22 17:01:53
Golang排列组合算法问题之全排列实现方法
2023-07-14 14:16:19
SQL Server 2000 清理日志精品图文教程
2012-07-21 14:31:17
如何使用SQLServer数据库查询累计值
2009-03-16 14:43:00
ASP中利用正则表达式实现论坛UBB代码转换
2008-02-29 11:49:00
设计表单的标签和输入区
2009-04-27 16:16:00
捕捉并保存ASP运行错误的函数代码
2012-11-30 20:24:43
Qzoneing主题视觉设计分享
2009-07-21 18:12:00
搞定web设计中网页路径问题
2007-09-22 08:52:00
ASP实现多域名同一空间的处理实例
2008-10-29 09:46:00
python中pyplot直方图的绘制方式
2023-11-20 07:58:17
简单的水果风暴的算法过程
2009-12-13 15:49:00
GO语言实现文件上传代码分享
2023-07-22 10:56:33
python中urllib.unquote乱码的原因与解决方法
2023-08-24 14:56:43
IE下中英文字体不能对齐原因及解决
2008-08-11 12:47:00
SQL根据时间范围条件查询数据
2009-01-18 13:32:00