python利⽤正则表达式搜索单词⽰例代码
前⾔
python正则表达式判断在python中,通过内嵌集成re模块,程序媛们可以直接调⽤来实现正则匹配。正则表达式模式被编译成⼀系列的字节码,然后由⽤C编写的匹配引擎执⾏。
⽐如下⾯的例⼦,就是⽤来从⼀段⽂字⾥查⼀个单词,如下:
⽰例代码
import re
pattern = 'this'
text = 'blog.csdn/caimouse is great, this is great way!'
match = re.search(pattern, text)
s = match.start()
e = d()
print('Found "{}"\nin "{}"\nfrom {} to {} ("{}")'.format(
结果输出如下:
Found "this"
in "blog.csdn/caimouse is great, this is great way!"
from 40 to 44 ("this")
在这⾥使⽤start()表⽰匹配的开始位置,end()表⽰结束位置。
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作能带来⼀定的帮助,如果有疑问⼤家可以留⾔交流,谢谢⼤家对的⽀持。