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取余运算符是什么?

Python求解物理学中的双弹簧质能系统的代码实例

Python os.chroot() 方法定义以及作用详解(实例)

Python如何安装wordcloud库

Python全栈要学什么

Python和go之间的区别是什么?

Python实现des加密解密的方法介绍(代码)

Python字典怎么排序

request timeout是什么意思

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




打赏

取消

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

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

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

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

评论

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