当前位置:首页 > Python教程 > python技巧

python基础语法21 re模块补充 正则表达式

正则表达式

              1
            import
             re

              2
              3
            print(re.findall(w, hello 123_ */-=))
  4print(len(re.findall(w, hello 123_ */-=)))
  5  6print(re.findall(W, hello 123_ */-=))
  7  8print(re.findall(s, hellno 12t3_ */-=))
  9 10print(re.findall(S, hellno 12t3_ */-=))
 11 12print(re.findall(d, hellno 12t3_ */-=))
 13print(re.findall(D, hellno 12t3_ */-=))
 14 15print(re.findall(n, hellno 12t3_ */-=))
 16print(re.findall(t, hellno 12t3_ */-=))
 17print(re.findall(l, hellno 12t3_ */-=))
 18 19print(re.findall(tank, my name is tank, tank is handsome))
 20print(re.findall(^tank, tank my name is tank, tank is handsome))
 21print(re.findall(tank$, tank my name is tank,tank is handsome tank))
 22 23重复匹配
 24.:匹配换行符以外的任意一个字符
 25 [abc,a1c,aac,aac,a*c,a+c]
 26print(re.findall(a.c, abc a1c aac asd aaaaac a*c a+c abasd))
 27a.c
 28print(re.findall(a.c, abc a1c aac anc asd aaaaac a*c a+c abasd, re.DOTALL))
 29 30[]: 匹配一个字符,该字符属于中括号内指定的字符
 31print(re.findall(a..c, abc a1 c aac asd aaaaac a *c a+c abasd =))
 32print(re.findall(a.c, abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c))
 33print(re.findall(a[a-z]c, abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c))
 34print(re.findall(a[A-Z]c, abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c))
 35 36 []内的+ * 不是量词
 37print(re.findall(a[-+*/]c, abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c))
 38print(re.findall(a[a-z][a-z]c, abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c))
 39 ^在[]内代表非
 40print(re.findall(a[^a-z]c, abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c))
 41 42 *: 必须与其他字符连用,代表左侧的字符出现0次或者无穷次
 43 ab* ---》 匹配0个或多个b字符, 并且是a开头的
 44print(re.findall(ab*, a ab abbb abbbb a1bbbb a-123))
 45# [‘a‘,‘ab‘,‘abbb‘,‘abbbb‘,‘a‘,‘a‘] 46 * == {0,}
 47print(re.findall(ab{0,}, a ab abbb abbbb a1bbbb a-123))
 48 49?: 必须与其他字符连用,代表左侧的字符出现0次或者1次
 50print(re.findall(ab?, a ab abbb abbbb a1bbbb a-123))
 51# ab? 52# [‘a‘,‘ab‘,‘ab‘,‘ab‘,‘a‘,‘a‘] 53 {0,1} == ?
 54print(re.findall(ab{0,1}, a ab abbb abbbb a1bbbb a-123))
 55 56 +: 必须与其他字符连用,代表左侧的字符出现1次或者无穷次
 57 ab+
 58print(re.findall(ab+, a ab abbb abbbb a1bbbb a-123))
 59# [‘ab‘,‘abbb‘,‘abbbb‘] 60 {1,} == +
 61print(re.findall(ab{1,}, a ab abbb abbbb a1bbbb a-123))
 62 63# {n,m}: 必须与其他字符连用 64 ab{1,3}  b字符出现1次——3次
 65print(re.findall(ab{1,3}, a ab abbb abb abbbb a1bbbb a-123))
 66# [‘ab‘, ‘abbb‘, ‘abb‘, ‘abbb‘] 67 68 69 .*:贪婪匹配
 70 a.*d ---> 匹配字符以最后的d作为结束标识
 71print(re.findall(a.*d, ab123adfc1134124123aasfc123123))
 72 73# .*?:非贪婪匹配 74 a.*?c
 75print(re.findall(a.*?c, ab123adfc1134124123adasfc123123))
 76 77():分组
 78 expression=".*?" 79print(re.findall(expression="(.*?)", expression="1+2+3/4*5" tank="handsome"))
 80print(re.findall(href="(.*?)",
 81<p>段落</p><a href="https://www.sb.com">点我啊</a><h1>标题</h1><a href="https://www.sb.com">点我啊</a>))
 82 83 a|b
 84print(re.findall(a|b, ab123abasdfaf))
 85 86 companies  company
 87(?:)表示非捕获分组,和捕获分组唯一的区别在于,非捕获分组匹配的值不会保存起来
 88 (?:)---> 将ies或者y保留与compan拼接
 89print(re.findall(compan(?:ies|y),
 90Too many companies have gone bankrupt, and the next one is my company))
 91 92标识性字符(提取的内容)
 93print(re.findall(ale(x), alex is SB,alex is bigSB))
 94print(re.search(alex, alex is SB,alex is bigSB).group())
 95print(re.search(abcdefg, alex is SB,alex is bigSB))
 96 97print(re.search(^alex, 123alex is SB,alex is bigSB))
 98print(re.match(alex, 123alex is SB,alex is bigSB))
 99100 l = tank:17:male.split(:)
101print(l)
102  了解: 根据“ ” or “:” or “/” or “-” 来进行切分
103 l1 = re.split([ :/-], a-b/c tank:17:male xxx)
104print(l1)
105106 [a-z]+xx
107 了解: sub: 替换  ---》 第二个参数 根据正则替换到第三参数中
108print(re.sub([a-z]+xx, yxp, lxx is good,sb is lllxx wxx is good cxx is good))
109110了解: compile可以将字符转成字节
111 pattern = re.compile(alex)  # 返回一个对象112print(pattern)
113print(pattern.findall(alex is SB,alex is bigSB))
114print(re.findall(alex, alex is SB,alex is bigSB))
115116print(pattern.search(alex is SB,alex is bigSB).group())  # alex117118import re
119120 str1 = 1abc a1 c aac aAcn taBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c121print(re.findall(w, str1))  # w---匹配字母数字及下划线122print(re.findall(W, str1))  # w---匹配非字母数字及下划线 n t123print(re.findall(s, str1))  # 匹配任意空白字符ntrf124print(re.findall(S, str1))  # 匹配非空白字符125print(re.findall(d, str1))  # 匹配数字等价0-9126print(re.findall(D, str1))  # 匹配任意非数字0-9127print(re.findall(Aac, str1))  # 匹配字母开始128print(re.findall(\nZ, str1))  # 匹配字母结束,只匹配到换行前的结束字符串129print(re.findall(n, str1))  # 匹配换行符130print(re.findall(t, str1))  # 匹配换行符131print(re.findall(^1abc, str1))  # 匹配以什么开头132print(re.findall(c$, str1))  # 匹配以什么结尾133134 str1 = 1abbb a1 anbc aac aAcn taBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c135print(re.findall(a.b, str1))  # 匹配中间是任意字符除了换行符136print(re.findall(a.b, str1, re.S))  # 匹配中间是任意字符包含换行符137print(re.findall(a.b, str1, re.DOTALL))  # 匹配中间是任意字符包含换行符138print(re.findall(ab*, str1))  # 匹配0个或多个表达式139print(re.findall(ab+, str1))  # 匹配1个或多个表达式140print(re.findall(ab?, str1))  # 匹配0个或1个表达式141print(re.findall(ab?a, str1))  # 匹配0个或1个表达式指代找b142print(re.findall(ab{2}, abbb aabxbaa))  # 表示1个a2个b143print(re.findall(a[1*-]b, a1b  anb a*b a-b))  # [‘a1b‘, ‘a*b‘, ‘a-b‘]144print(re.findall(a[^1*-]b, a1b a*b a-b a=b))  # []内的^表示取反145print(re.findall(a[0-9]b, a1b a*b a-b a=b))  # [‘a1b‘]146print(re.findall(a[a-z]b, a1b a*b a-b a=b aeb))  # [‘aeb‘]147print(re.findall(a[a-zA-Z]b, a1b a*b a-b a=b aeb aEb))  # [‘aeb‘, ‘aEb‘]148print(re.findall(ra\c, ac))
149print(re.findall((ab)+123, ababab123))
150print(re.findall((?:ab)+123, xxxaab123))  # [‘ab123‘]151print(re.findall((?:ab)+123, 12abab123))  # [‘abab123‘]如果有相同的ab连接在一起就一起显示152print(re.findall(compan(?:ies|y), Too many companies have gone bankrupt, and the next one is my company))
153print(re.findall(href="(.*?)",
154<p>段落</p><a href="https://www.sb.com">点我啊</a><h1>标题</h1><a href="https://www.sb.com">点我啊</a>))
155print(re.findall(a|b, ab123abasdfaf))
156print(re.split(ab, abcd))  # [‘‘, ‘cd‘]157print(re.split([ab], abcd))  # [‘‘, ‘‘, ‘cd‘] #如果是列表按照索引取158print(===>, re.sub(a, A, alex make love))  # ===> Alex mAke love,不指定n,默认替换所有159print(===>, re.sub(a, A, alex make love, 1))  # ===> Alex make love160161 obj = re.compile(d{3})  # 查找3个数字还要连续的162print(obj.search(abc123eee1e).group())  # 12163print(obj.findall(abc123eeee))  # [‘12‘],重用了obj164165print(re.findall(a,b|c, ac,a,b,accc))
166print(re.findall(ab?, a))
167168import re
169170print(re.findall("<(?P<tag_name>w+)>w+</(?P=tag_name)>", "<h1>hello</h1>"))  # [‘h1‘]171172173import re
174175 str1 = <h1>www.oldboyedu.*+com<h1>176177# www.oldboyedu.178re.findall(
179# . == ‘.‘180# . == 任意字符181www.*.*+,
182    str1
183 )

原文:https://www.cnblogs.com/ludingchao/p/12118141.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐