好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

正则表达式本身是一种小型的、高度专业化的编程语言,下面这篇文章主要给大家介绍了关于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()表示结束位置。

总结

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

查看更多关于python使用正则表达式实现搜索单词的示例代码的详细内容...

  阅读:37次