re模块[Python常见问题]

re模块

1、什么是正则表达式?什么是re模块?

  (1)正则表达式:

    正则表达式是一门独立的技术,人和语言都可以使用正则表达式,

    正则表达式是由一堆特殊的字符组合而来的

      ① 元字符:

        ^:代表开头

        $:代表结束

        |:或者的意思

        ():可以获取一个值,判断是否是13或14等

        {9}:需要获取9个值

        []:分组限制取值范围,[0-9]:限制只能获取0-9的某一个字符

        参考图片:点我查看

import re

# w:匹配字母、数字、下划线
print(re.findall("w", "hello 123_ */-="))    # 执行结果:["h", "e", "l", "l", "o", "1", "2", "3", "_"]
# W:匹配非字母、数字、下划线
print(re.findall("W", "hello 123_ */-="))    # 执行结果:[" ", " ", "*", "/", "-", "="]
# s:匹配任意空白字符
print(re.findall("s", "hell
o 12	3_ */-="))    # 执行结果:["
", " ", "	", " "]
# S:匹配任意非空字符
print(re.findall("S", "hell
o 12	3_ */-="))    # 执行结果:["h", "e", "l", "l", "o", "1", "2", "3", "_", "*", "/", "-", "="]
# d:匹配任意数字,等价于[0-9]
print(re.findall("d", "hell
o 12	3_ */-="))    # 执行结果:["1", "2", "3"]
# D:匹配任意非数字
print(re.findall("D", "hell
o 12	3_ */-="))    # 执行结果:["h", "e", "l", "l", "
", "o", " ", "	", "_", " ", "*", "/", "-", "="]
# 
:匹配一个换行符
print(re.findall("
", "hell
o 12	3_ */-="))    # 执行结果:["
"]
# 	:匹配一个制表符
print(re.findall("	", "hell
o 12	3_ */-="))    # 执行结果:["	"]
# 匹配特定字符
print(re.findall("l", "hell
o 12	3_ */-="))    # 执行结果:["l", "l"]
print(re.findall("yangy", "yangy my name is yangy, oh yangy is my big baby"))    # 执行结果:["yangy", "yangy", "yangy"]
# ^:以什么开头
print(re.findall("^yangy", "yangy my name is yangy, oh yangy is my big baby"))    # 执行结果:["yangy"]
# $:以什么结尾
print(re.findall("yangy$", "yangy my name is yangy, oh yangy is my big baby, yangy"))    # 执行结果:["yangy"]
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » re模块