Python文本处理

搜索和匹配

搜索和匹配详细操作教程
使用正则表达式有两个基本操作看起来相似但有显着差异。 re.match()仅在字符串的开头检查匹配,而re.search()检查字符串中任何位置的匹配。 这在文本处理中起着重要作用,因为通常必须编写正确的正则表达式来检索用于情感分析的文本块作为示例。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
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"
当我们运行上面的程序时,得到以下输出 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-23
1. search result found anywhere in the string
2. Match with beginning of string
3. No match with match if not beginning
4. search as match
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4