"""
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.A
re.ASCII
re.DEBUG
re.S
re.DOTALL
re.X
re.VERBOSE
re.M
re.MULTILINE
re.I
re.IGNORECASE
re.U
re.UNICODE
str_test = "ab中"
# \w: 匹配大小写字母数字_: 如果是unicode的情况,它可以匹配中文
pattern = "\w+"
match_obj = re.match(pattern, str_test)
print(match_obj)
# re.A = = re.ASCII
match_obj = re.match(pattern, str_test, flags=re.A)
print(match_obj)
str_test = "xxx"
pattern = "x"
# re.DEBUG: 打印调试信息
match_obj = re.match(pattern, str_test, flags=re.DEBUG)
# re.I == re.IGNORECASE 忽略大小写
str_test = "AcBd" #ABCD
pattern = "acbd"
match_obj = re.match(pattern, str_test)
print(match_obj)
match_obj = re.match(pattern, str_test, re.I)
print(match_obj)
# re.S == re.DOTALL
# 正则表达式中.字符:匹配任意一个字符,但不包括换行"\n"
# re.S 匹配任意一个字符,也包括换行"\n"
str_test = "ABcd\n"
pattern = "....."
match_obj = re.match(pattern, str_test)
print(match_obj)
match_obj = re.match(pattern, str_test, re.S)
print(match_obj)
# re.VERBOSE == re.X
# 对正则表达式进行说明 #xxxx
print("*" * 80)
pattern = "abcd# 匹配ABCD"
str_data = "abcd"
match_obj = re.match(pattern, str_data, re.X)
print(match_obj)
# unicode: 定长的编码 2个字节 中文编码:[u4e00-u9fa5]
# re.U = re.Unicode => Python3默认使用unicode
str_data = "\u4e00"
str_data = "\u4fa5"
pattern = "\w"
match_obj = re.match(pattern, str_data)
print(match_obj)
# re.M == re.MULTILINE 多行
str_data = "123111\n123111"
pattern = "^123" # ^ 代表以...开始的字符串
list_data = re.findall(pattern, str_data)
print(list_data)
list_data = re.findall(pattern, str_data, re.M)
print(list_data)
# 123111
# 123111
# 123
Python(re_cost_learning)
最新推荐文章于 2024-08-26 18:56:03 发布