python使用正则表达式实现搜索单词的示例代码


当前第2页 返回上一页

示例代码


1

2

3

4

5

6

7

8

9

10

11

12

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]))

结果输出如下:


1

2

3

Found "this"

in "http://blog.csdn.net/caimouse is great, this is great way!"

from 40 to 44 ("this")

在这里使用start()表示匹配的开始位置,end()表示结束位置。

总结

以上就是python使用正则表达式实现搜索单词的示例代码的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

Python安装扩展库常用的是什么工具

Python如何调用c语言函数的使用详解

回味Python基本数据类型

Python怎么学得快

Python的单线程多任务的实现

Python如何批量处理excel数据?

Python中定义类的关键字是什么

Python中*的用法介绍(代码示例)

利用Python如何爬取js里面的内容

理解Python的全局变量和局部变量

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...