1+ import re
2+
3+ #-- 基本正则表达式语法
4+ '''
5+ .:匹配除换行符以外的任意字符。
6+ ^:匹配字符串的开始。
7+ $:匹配字符串的结束。
8+ *:匹配前一个字符0次或多次。
9+ +:匹配前一个字符1次或多次。
10+ ?:匹配前一个字符0次或1次。
11+ {m}:匹配前一个字符m次。
12+ {m,n}:匹配前一个字符至少m次,至多n次。
13+ [abc]:匹配字符集合中的任意一个字符(例如a、b或c)。
14+ [^abc]:匹配不在字符集合中的任意字符。
15+ |:表示“或”关系,匹配符号前后的任意一个表达式。
16+ ():用于分组,可以提取匹配的部分。
17+ \d:匹配任意数字,等价于[0-9]。
18+ \D:匹配任意非数字字符,等价于[^0-9]。
19+ \w:匹配任意字母数字字符,包括下划线,等价于[a-zA-Z0-9_]。
20+ \W:匹配任意非字母数字字符,等价于[^a-zA-Z0-9_]。
21+ \s:匹配任意空白字符,包括空格、制表符、换行符等。
22+ \S:匹配任意非空白字符。
23+ '''
24+
25+ #-- re 模块的主要函数
26+ re .match (pattern , string , flags = 0 ) # 尝试从字符串的起始位置匹配正则表达式,如果匹配成功,返回一个匹配对象;否则返回None。
27+ re .search (pattern , string , flags = 0 ) # 扫描整个字符串,返回第一个成功匹配的对象;否则返回None。
28+ re .findall (pattern , string , flags = 0 ) # 在字符串中找到所有与正则表达式匹配的非重叠匹配项,并返回一个列表。
29+ re .finditer (pattern , string , flags = 0 ) # 与findall类似,但返回的是一个迭代器,每个迭代元素都是一个匹配对象。
30+ re .sub (pattern , repl , string , count = 0 , flags = 0 ) # 使用repl替换string中所有与pattern匹配的子串,count表示替换的最大次数。
31+ re .subn (pattern , repl , string , count = 0 , flags = 0 ) # 功能与sub相同,但返回一个元组,包含替换后的字符串和替换的总次数。
32+ re .split (pattern , string , maxsplit = 0 , flags = 0 ) # 根据正则表达式的模式分割字符串,maxsplit表示分割的最大次数。
33+ re .escape (string ) # 对字符串中的非字母数字字符进行转义,使其可以安全地用于正则表达式中。
34+
35+ #-- 示例代码1
36+ # 匹配字符串的开始
37+ match_obj = re .match (r'^Hello' , 'Hello World' )
38+ if match_obj :
39+ print ("Match found:" , match_obj .group ())
40+ else :
41+ print ("No match" )
42+
43+ # 搜索字符串
44+ search_obj = re .search (r'World' , 'Hello World' )
45+ if search_obj :
46+ print ("Search found:" , search_obj .group ())
47+ else :
48+ print ("No search match" )
49+
50+ # 查找所有匹配项
51+ findall_result = re .findall (r'\d+' , 'There are 42 apples and 33 oranges' )
52+ print ("Findall result:" , findall_result )
53+
54+ # 替换匹配项
55+ sub_result = re .sub (r'\d+' , 'many' , 'There are 42 apples and 33 oranges' )
56+ print ("Sub result:" , sub_result )
57+
58+ # 分割字符串
59+ split_result = re .split (r'\s+' , 'Hello World this is Python' )
60+ print ("Split result:" , split_result )
61+
62+ # 转义字符串
63+ escaped_string = re .escape ('Hello? World!' )
64+ print ("Escaped string:" , escaped_string )
65+
66+ #-- 标志位(flags)
67+ re .IGNORECASE # 忽略大小写。
68+ re .MULTILINE # 多行模式,改变^和$的行为。
69+ re .DOTALL # 让.匹配包括换行符在内的所有字符。
70+ re .VERBOSE # 允许正则表达式包含空白和注释,使其更易于阅读。
71+
72+ #-- 示例代码2
73+ pattern = re .compile (r'''
74+ \d+ # 匹配一个或多个数字
75+ \. # 匹配小数点
76+ \d+ # 匹配一个或多个数字
77+ ''' , re .VERBOSE )
78+
79+ match_obj = pattern .match ('123.456' )
80+ if match_obj :
81+ print ("Match found:" , match_obj .group ())
82+ else :
83+ print ("No match" )
0 commit comments