Python(regex_regular_learning)

"""
正则表达式:regular expression => re
在Python中也支持正则表达式:Java, Shell, Python
正则表达式比较重要:在处理文本的时候, 很多时候要搜索一些东西,使用正则表达式
                表单验证的时候:验证手机号,身份证,邮箱,密码
在Python中支持正则表达式的模块叫做 re
在Python中使用正式表达式: import re
re模块中的模式常量:
        A = 0
        ASCII = 0
        DEBUG = 0
        I = 0
        IGNORECASE = 0
        L = 0
        LOCALE = 0
        M = 0
        MULTILINE = 0
        S = 0
        DOTALL = 0
        X = 0
        VERBOSE = 0
        U = 0
        UNICODE = 0
        T = 0
        TEMPLATE = 0

"""
import re

# re中的函数
# re.match()
"""
def match(pattern, string, flags=0):
    尝试将一个pattern匹配字符串的开始, 返回Match Object
    如果匹配不到,返回空
match: 匹配
pattern: 正则表达式
string: 字符串(待匹配的目标)
flags: 默认值是0-> 不使用任何的flag
功能: 匹配字符串是否以正则表达式开始
     字符串:abcd 
     正则表达式: abc, abcd, a, ab, bcd

"""
str_test = "abcd"
pattern = "abc"
match_obj = re.match(pattern, str_test)
print(match_obj)
print(match_obj.span())
print(match_obj.start())
print(match_obj.end())
print(match_obj.groupdict())
print(match_obj.group())
print(match_obj.group(0))
print(match_obj.groups())
print(match_obj.endpos)
print(match_obj.string)
print(match_obj.pos)
# 分解match_obj
# print(dir(match_obj))
# span=(0, 3)

# re.search()
"""
def search(pattern, string, flags=0):
    
    通过扫描字符串查找一个和正则表达式匹配的内容,返回Match object, 匹配不到为空
search: 搜索
pattern: 正则表达式
string: 待匹配的字符串
flag: 模式
功能: 从任意位置搜索正则表达式
string: abcd
pattern: bc, bcd, abc, ab, cd
"""
match_obj = re.search("bd", "abcd")
print(match_obj)

#re.fullmatch() #完全匹配
"""
def fullmatch(pattern, string, flags=0):
    用正则表达式完全去匹配字符串,匹配到了返回Match object, 匹配不到返回None
    正则表达式 == 字符串
"""
pattern = "abcd"
str_test = "abcd"
match_obj = re.fullmatch(pattern, str_test)
print(match_obj)

# re.findall()
"""
def findall(pattern, string, flags=0):
    返回一个列表(匹配到所有的结果), 列表中的元素都是字符串
    功能:去查找所有的
"""
str_test = "abcdefabchlabclsdjfksabc"
pattern = "abc"
data = re.findall(pattern, str_test)
print(data)
# re.finditer() #
"""
def finditer(pattern, string, flags=0):
    返回一个迭代器,且迭代器的每一个元素都是Match object  
"""
str_test = "abcdefabchlabclsdjfksabc"
pattern = "abc"
data = re.finditer(pattern, str_test)
print(data)
for i in data:
    print(i)

# re.split() split: 分割
"""
def split(pattern, string, maxsplit=0, flags=0):
    Split the source string by the occurrences of the pattern,
    returning a list containing the resulting substrings.
    pattern: 正则表达式
    string: 待匹配的字符串
    maxsplit: 最大分割次数
    flags: 模式
    使用正则表达式去分割字符串, 先用正则匹配匹配到出现的内容,然后根据匹配到的内容去分割
    
"""
str_test = "软工,计算机,网络,自动化,信息工程"
pattern = ","
data = re.split(pattern, str_test)
print(data)
data = re.split(pattern, str_test, maxsplit=3)
print(data)

# 替换
# re.sub()
"""
def sub(pattern, repl, string, count=0, flags=0):
    pattern: 正则表达式
    repl:replace: 替换, 替换之后的内容
    string: 待匹配的字符串
    count:计数:替换的次数
    flags: 模式
    返回一个字符串:
    使用替换字符串去替换待匹配的字符串中出现的pattern
    count:用来指定替换次数 

"""
str_test = "软工,计算机,网络,自动化,信息工程"
pattern = ","
repl = "-"
data = re.sub(pattern, repl, str_test)
print(data)
data = re.sub(pattern, repl, str_test, count=3)
print(data)
# re.subn() # sub + number
str_test = "软工,计算机,网络,自动化,信息工程"
pattern = ","
repl = "-"
data = re.subn(pattern, repl, str_test)
print(data)
# compile: 编译

# re.compile()

str_test = "abcd"
pattern = "abc"
re.match(pattern, str_test)
#_compile(pattern, flags).match(string)
re.fullmatch(pattern, str_test)
#_compile(pattern, flags).fullmatch(string)
re.search(pattern, str_test)
#_compile(pattern, flags).search(string)
re.finditer(pattern, str_test)
#_compile(pattern, flags).finditer(string)

# _complie(pattern, flags)调用了四次
# _complie(pattern, flags) => 拿到这个对象
# 对象.match,对象.fullmatch, 对象.search, 对象.finditer
# 执行了一次_complie(pattern, flags), 提高了效率

re.compile() # 就是编译正则表达式,然后拿到一个编译后的对象
# 一次编译,多次使用
"""
def compile(pattern, flags=0):
    "Compile a regular expression pattern, returning a Pattern object."
    编译一个正则表达式,返回一个Pattern object
"""
str_test = "abcd"
pattern = "abc"

pattern_obj = re.compile(pattern)
pattern_obj.match(pattern, str_test)
pattern_obj.fullmatch(pattern, str_test)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值