我爱模板网 > 建站教程 > Python教程 >  Python - 搜索和匹配正文

Python - 搜索和匹配

使用正则表达式有两个基本操作看起来相似但有显着差异。该 re.match() 检查只在字符串的开头匹配,而 re.search() 检查匹配字符串中的任何地方。这在文本处理中起着重要作用,因为我们通常必须编写正确的正则表达式来检索用于情感分析的文本块作为示例。

import re

if  re.search("tor", "Tutorial"):
        print "1. search result found anywhere in the string"

if re.match("Tut", "Tutorial"):
         print "2. Match with beginning of string"

if not re.match("tor", "Tutorial"):
        print "3. No match with match if not beginning"



# Search as Match

if  not re.search("^tor", "Tutorial"):
        print "4. search as match"


部分素材资源来源网站,本站提供免费下载,如有侵权请联系站长马上删除!
上一篇:Python - 文本Munging 下一篇:Python - 情感分析
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
选择头像:
最新评论

猜你喜欢