【正则表达式高级day02】re模块操作+re模块的使用过程+re模块示例(匹配以itcast开头的语句)

re模块操作

在Python中需要通过正则表达式对字符串进行匹配的时候,可以使用一个模块,名字为re

1. re模块的使用过程

    #coding=utf-8

    # 导入re模块
    import re

    # 使用match方法进行匹配操作
    result = re.match(正则表达式,要匹配的字符串)

    # 如果上一步匹配到数据的话,可以使用group方法来提取数据
    result.group()

2. re模块示例(匹配以itcast开头的语句)

    #coding=utf-8

    import re

    result = re.match("itcast","itcast.cn")

    result.group()

运行结果为:

itcast

3. 说明

  • re.match() 能够匹配出以xxx开头的字符串
发布了120 篇原创文章 · 获赞 19 · 访问量 4069

猜你喜欢

转载自blog.csdn.net/qq_35456045/article/details/104072990