(python)正则表达式
# -*- coding: utf-8 -*- import re def test_dot(): """ . => 匹配单个任意字符 """ str1 = "woodphogrammhootoohoingtesthoohhhkachhraghello\nworldtadhhtesttwhat1b2d3cohhhhh" print(re.findall("t..t", str1)) # ['test', 'test'] str2 = "test\n" print(re.findall(".", str2)) # ['t', 'e', 's', 't'] print(re.findall(".", str2, flags=re.DOTALL)) # 标记 DOTALL 是将 \n 当成字符处理 # ['t', 'e', 's', 't', '\n'] def test_caret(): """ ^ => 1) 匹配字符串的开头 2)如 flags = MULTILINE,则匹配每一行的开头 3)[^] 放在[],表示取反操作 """ str1 = "woodphogrammhootoohoingtesthoohhhkachhraghello\nworldtadhhtesttwhat1b2d3cohhhhh" print(re.findall("^w..", str1)) # ['woo'] print(re.findall("^w..", str1, flags=re.MULTILINE)) # ['woo', 'wor'] print(re.findall("[^b-z][a-d]", str1)) # ['ac', 'ad', '1b', '2d', '3c'] def test_squareBrackets(): """ [] => 1) 单独列出字符,如 [abc],表示"a", "b", "c" 2)- 分割,表示字符串范围,如 [a-z] 表示匹配任何 a-z 的字符,[0-5][0-9] 表示匹配 00-59 的数字 3)特殊符号在[] 将失去意义,如 ['(', '*', '+', ')']将匹配字符 '(', '*', '+', ')' """ print(re.findall("[]abc]", "afdhhhbggg")) # ['a', 'b'] print(re.findall("[a-h]", "zebraisaanimal")) # ['e', 'b', 'a', 'a', 'a', 'a'] print(re.findall("[(+*)]", "abcdefdfd(/ifdfd4343*re+4jjfd)8999")) # ['(', '*', '+', ')'] def test_dollar(): """ $ => 匹配字符串的结尾或正好在字符串末尾的换行符之前,并且在多行模式下也匹配新行之前的内容 """ str1 = "foo" str2 = "foobar" print(re.findall("foo", str1), re.findall("foo", str2)) # ['foo'] ['foo'] print(re.findall("foo$", str1), re.findall("foo$", str2)) # ['foo'] [] str3 = "foo1\nfoo2\n" print(re.findall("foo.$", str3)) # ['foo2'] print(re.findall("foo.$", str3, flags=re.MULTILINE)) # ['foo1', 'foo2'] print(re.findall("$", "foo\n")) # ['', '']:两个空匹配项,一个在新行前,一个在字符串末尾 def test_asterisk(): """ * => 匹配0 或多个字符 """ str1 = "woodphogrammhootoohoingtesthoohhhkachhraghello\nworldtadhhtesttwhat1b2d3cohhhhh" print(re.findall("ho*", str1)) # ['ho', 'hoo', 'ho', 'hoo', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h'], h 开头,有 0~n 个o def test_plus(): """ + => 匹配字符重复一次或多次 如 "ab+" 表示 匹配a ,后面有 非零个 b """ str1 = "woodphogrammhootoohoingtesthoohhhkachhraghello\nworldtadhhtesttwhat1b2d3cohhhhh" print(re.findall("ho+", str1)) # ['ho', 'hoo', 'ho', 'hoo'] def test_interrogation(): """ ? => 匹配一个字符0或1次, 用于防止贪婪匹配 """ str1 = "woodphogrammhootoohoingtesthoohhhkachhraghello\nworldtadhhtesttwhat1b2d3cohhhhh" print(re.findall("ho?", str1)) # ['ho', 'ho', 'ho', 'ho', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h', 'h'] def test_brace(): """ {} => 范围 1) {m} : 匹配前一个字符 m 次; {m,n}:匹配前一个字符 m~n 次; {m~}:匹配前一个字符 m 至无限次 2) {0,} :匹配前一个字符 0 或 多次,等同于 * 3) {1,} : 匹配前一个字符 1 或 多次,等同于 + 4) {0,1}:匹配前一个字符 0 或 1 次,等同于 ? 注意: , 的前后都不能有空格 """ str1 = "woodphogrammhootoohoingtesthoohhhkachhraghello\nworldtadhhtesttwhat1b2d3cohhhhh" print(re.findall("h{1,}", str1)) # ['h', 'h', 'h', 'h', 'h', 'h'] print(re.findall("h+", str1)) # ['h', 'h', 'h', 'h', 'hhh', 'hh', 'h', 'hh', 'h', 'hhhhh'] print(re.findall("h{0,}", str1)) # ['', '', '', '', '', 'h', '', '', '', '', '', '', 'h', '', '', '', '', '', 'h', '', '', '', '', '', '', '', '', # 'h', '', '', 'hhh', '', '', '', 'hh', '', '', '', 'h', '', '', '', '', '', '', '', '', '', '', '', '', '', # 'hh', '', '', '', '', '', '', 'h', '', '', '', '', '', '', '', '', '', 'hhhhh', ''] print(re.findall("h*", str1)) # ['', '', '', '', '', 'h', '', '', '', '', '', '', 'h', '', '', '', '', '', 'h', '', '', '', '', '', '', '', '', # 'h', '', '', 'hhh', '', '', '', 'hh', '', '', '', 'h', '', '', '', '', '', '', '', '', '', '', '', '', '', # 'hh', '', '', '', '', '', '', 'h', '', '', '', '', '', '', '', '', '', 'hhhhh', ''] print(re.findall("h{0,1}", str1)) # ['', '', '', '', '', 'h', '', '', '', '', '', '', 'h', '', '', '', '', '', 'h', '', '', '', '', '', '', '', '', # 'h', '', '', 'h', 'h', 'h', '', '', '', 'h', 'h', '', '', '', 'h', '', '', '', '', '', '', '', '', '', '', '', # '', '', 'h', 'h', '', '', '', '', '', '', 'h', '', '', '', '', '', '', '', '', '', 'h', 'h', 'h', 'h', 'h', ''] print(re.findall("h?", str1)) # ['', '', '', '', '', 'h', '', '', '', '', '', '', 'h', '', '', '', '', '', 'h', '', '', '', '', '', '', '', '', # 'h', '', '', 'h', 'h', 'h', '', '', '', 'h', 'h', '', '', '', 'h', '', '', '', '', '', '', '', '', '', '', '', # '', '', 'h', 'h', '', '', '', '', '', '', 'h', '', '', '', '', '', '', '', '', '', 'h', 'h', 'h', 'h', 'h', ''] def test_parentheses(): """ () => 分组匹配,() 里面的是一个组 """ str1 = "woodphogrammhootoohoingtesthoohhhkachhraghello\nworldtadhhtesttwhat1b2d3cohhhhh" print(re.findall("h(.+?)t", str1)) # ['ogrammhoo', 'oing', 'h', 'a'] def test_char(): """ \d :任意十进制数,等同于 [0-9] \d+ :匹配一位或多位数字 \D : 匹配任意非数字字符, 相当于[^0-9] \s :匹配任意空白字符,相当于 [\t\n\r\f\v] \S : 匹配任意非空白字符,相当于 [^\t\n\r\f\v] \w : 匹配包括下划线在内的任意字母、数字、字符,相当于 [a-zA-Z0-9] \W : 匹配包括下划线在内的任意非字母、数字、字符,相当于 [^a-zA-Z0-9] | : 表示或,前后其中一个符合就匹配 """ str3 = "1hello2world3test4linux5python" print(re.findall("\d", str3)) # ['1', '2', '3', '4', '5'] str1 = "woodphogrammhootoohoingtesthoohhhkachhraghello\nworldtadhhtesttwhat1b2d3cohhhhh" print(re.findall("h|t.", str1)) # h 或 t. (t后面任意字符) # ['h', 'h', 'to', 'h', 'te', 'th', 'h', 'h', 'h', 'h', 'h', 'h', 'ta', 'h', 'h', 'te', 'tt', 'h', 't1', 'h', 'h', 'h', 'h', 'h'] def test_lazy(): """ 懒惰限定符, *? => 重复任意次,但尽可能少重复 +? => 重复 1次或更多次,但尽可能少重复 ?? => 重复 0次或1次,但尽可能少重复 """ str2 = "acbacebacdb" print(re.findall("a.*b", str2)) # ['acbacebacdb'] print(re.findall("a.*?b", str2)) # ['acb', 'aceb', 'acdb'] print(re.findall("a.+?b", str2)) # ['acb', 'aceb', 'acdb'] print(re.findall("a.??b", str2)) # ['acb'] if __name__ == '__main__': test_dot() test_caret() test_squareBrackets() test_dollar() test_asterisk() test_plus() test_interrogation() test_brace() test_parentheses() test_char() test_lazy()
======================
转载:https://www.cnblogs.com/lj-C/p/14899924.html
一、正则表达式简介
正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。 Python 自1.5版本起增加了re 模块,它提供 Perl
风格的正则表达式模式。
就其本质而言,正则表达式(或 RE)是一种小型的、高度专业化的编程语言, (在Python中)它内嵌在Python中,并通过 re
模块实现。正则表达式模式被 编译成一系列的字节码,然后由用 C 编写的匹配引擎执行。
re 模块使 Python 语言拥有全部的正则表达式功能。 compile
函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象。该对象拥有一系列方法用于正则表达式匹配和替换。 re
模块也提供了与这些方法功能完全一致的函数,这些函数使用一个模式字符串做为它们的第一个参数。
二、字符匹配
1.普通字符:大多数字符和字母都会和自身匹配
>>> re.findall("alexsel","gtuanalesxalexselericapp") ['alexsel'] >>> re.findall("alexsel","gtuanalesxalexswxericapp") [] >>> re.findall("alexsel","gtuanalesxalexselwupeiqialexsel") ['alexsel', 'alexsel']
2.元字符: . ^ $ * + ? { } [ ] | ( ) \
•. :匹配一个除了换行符任意一个字符
>>> re.findall("alexsel.w","aaaalexselaw") ['alexselaw'] #一个点只能匹配一个字符
•^ :只有后面跟的字符串在开头,才能匹配上
>>> re.findall("^alexsel","gtuanalesxalexselgeappalexsel")
[]
>>> re.findall("^alexsel","alexselgtuanalesxalexselwgtappqialexsel")
['alexsel']
#"^"这个符号控制开头,所以写在开头
•$ :只有它前面的字符串在检测的字符串的最后,才能匹配上
>>> re.findall("alexsel$","alexselseguanalesxalexselganapp")
[]
>>> re.findall("alexsel$","alexselgtaanalesxalexsssiqialexsel")
['alexsel']
•* :它控制它前面那个字符,他前面那个字符出现0到多个都可以匹配上
>>> re.findall("alexsel*","aaaalexse")
['alexse']
>>> re.findall("alexsel*","aaaalexsel")
['alexsel']
>>> re.findall("alex*","aaaalexsellllll")
['alexsellllll']
•+ :匹配前面那个字符1到多次
>>> re.findall("alexsel+","aaaalexselll")
['aleselll']
>>> re.findall("alexsel+","aaaalexsel")
['alexsel']
>>> re.findall("alexsel+","aaaalexse")
[]
•? :匹配前面那个字符0到1个,多余的只匹配一个
>>> re.findall("alexsel?","aaaalexse")
['alexse']
>>> re.findall("alexsel?","aaaalexsel")
['alexsel']
>>> re.findall("alexsel?","aaaalexsellll")
['alexsel']
•{} :控制它前面一个字符的匹配个数,可以有区间(闭区间),有区间的情况下按照多的匹配
>>> re.findall("alexsel{3}","aaaalexselllll")
['alexselll']
>>> re.findall("alexsel{3}","aaaalexsell")
[]
>>> re.findall("alexsel{3}","aaaalexse")
[]
>>> re.findall("alexsel{3}","aaaalexselll")
['alexselll']
>>> re.findall("alexsel{3,5}","aaaalexsellllllll")
['alexselllll']
>>> re.findall("alexsel{3,5}","aaaalexselll")
['alexselll']
>>> re.findall("alexsel{3,5}","aaaalexsell")
[]
•\ :
后面跟元字符去除特殊功能,
后面跟普通字符实现特殊功能。
引用序号对应的字组所匹配的字符串 (一个括号为一个组)。
在开头加上 r 表示不转义。
#\2 就相当于第二个组(eric)
>>> re.search(r"(alexsel)(eric)com\2","alexselericcomeric").group()
'alexselericcomeric'
>>> re.search(r"(alexsel)(eric)com\1","alexselericcomalex").group()
'alexselericcomalex'
>>> re.search(r"(alexsel)(eric)com\1\2","alexselericcomalexseleric").group()
'alexselericcomalexeric'
\d :匹配任何十进制数;它相当于类[0-9]
>>> re.findall("\d","aaazz1111344444c")
['1', '1', '1', '1', '3', '4', '4', '4', '4', '4']
>>> re.findall("\d\d","aaazz1111344444c")
['11', '11', '34', '44', '44']
>>> re.findall("\d0","aaazz1111344444c")
[]
>>> re.findall("\d3","aaazz1111344444c")
['13']
>>> re.findall("\d4","aaazz1111344444c")
['34', '44', '44']
\D :匹配任何非数字字符;它相当于类[^0-9]
>>> re.findall("\D","aaazz1111344444c")
['a', 'a', 'a', 'z', 'z', 'c']
>>> re.findall("\D\D","aaazz1111344444c")
['aa', 'az']
>>> re.findall("\D\d\D","aaazz1111344444c")
[]
>>> re.findall("\D\d\D","aaazz1z111344444c")
['z1z']
\s :匹配任何空白字符;它相当于类[ \t\n\r\f\v]
>>> re.findall("\s","aazz1 z11..34c")
[' ']
\S :匹配任何非空白字符;它相当于类[^ \t\n\r\f\v]
\w :匹配任何字母数字字符;他相当于类[a-zA-Z0-9_]
>>> re.findall("\w","aazz1z11..34c")
['a', 'a', 'z', 'z', '1', 'z', '1', '1', '3', '4', 'c']
\W :匹配任何非字母数字字符;它相当于类[^a-zA-Z0-9_]
\b :匹配一个单词边界,也就是指单词和空格间的位置
>>> re.findall(r"\babc\b","abc sdsadasabcasdsadasdabcasdsa")
['abc']
>>> re.findall(r"\balexsel\b","abc alexsel abcasdsadasdabcasdsa")
['alexsel']
>>> re.findall("\\balexsel\\b","abc alexsel abcasdsadasdabcasdsa")
['alexsel']
>>> re.findall("\balexsel\b","abc alexsel abcasdsadasdabcasdsa")
[]
() :把括号内字符作为一个整体去处理
>>> re.search(r"a(\d+)","a222bz1144c").group()
'a222'
>>> re.findall("(ab)*","aabz1144c")
['', 'ab', '', '', '', '', '', '', ''] #将括号里的字符串作为整和后面字符逐个进行匹配,在这里就首先将后面字符串里的a和ab进
#行匹配,开头匹配成功,在看看后面是a,和ab中的第二个不匹配,然后就看后面字符串中的第二个a,和ab匹配,首先a匹配成功,b也匹配成功,拿到匹配
#然后在看后面字符串中的第三个是b,开头匹配失败,到第四个,后面依次
>>> re.search(r"a(\d+)","a222bz1144c").group()
'a222'
>>> re.search(r"a(\d+?)","a222bz1144c").group() +的最小次数为1
'a2'
>>> re.search(r"a(\d*?)","a222bz1144c").group() *的最小次数为0
'a'
#非贪婪匹配模式 加? ,但是如果后面还有匹配字符,就无法实现非贪婪匹配
#(如果前后均有匹配条件,则无法实现非贪婪模式)
>>> re.findall(r"a(\d+?)b","aa2017666bz1144c")
['2017666']
>>> re.search(r"a(\d*?)b","a222bz1144c").group()
'a222b'
>>> re.search(r"a(\d+?)b","a277722bz1144c").group()
'a277722b'
元字符在字符集里就代表字符,没有特殊意义(有几个例外)
>>> re.findall("a[.]d","aaaacd")
[]
>>> re.findall("a[.]d","aaaa.d")
['a.d']
例外: [-] [^] []
[-]
#匹配单个字符,a到z所有的字符
>>> re.findall("[a-z]","aaaa.d")
['a', 'a', 'a', 'a', 'd']
>>> re.findall("[a-z]","aaazzzzzaaccc")
['a', 'a', 'a', 'z', 'z', 'z', 'z', 'z', 'a', 'a', 'c', 'c', 'c']
>>>
>>> re.findall("[1-3]","aaazz1111344444c")
['1', '1', '1', '1', '3']
[^]
#匹配除了这个范围里的字符,(^在这里有 非 的意思)
>>> re.findall("[^1-3]","aaazz1111344444c")
['a', 'a', 'a', 'z', 'z', '4', '4', '4', '4', '4', 'c']
>>> re.findall("[^1-4]","aaazz1111344444c")
['a', 'a', 'a', 'z', 'z', 'c']
[\]
>>> re.findall("[\d]","aazz1144c")
['1', '1', '4', '4']
我们首先考察的元字符是"[" 和 "]"。它们常用来指定一个字符类别,所谓字符类
别就是你想匹配的一个字符集。字符可以单个列出,也可以用“-”号分隔的两个给定 字符来表示一个字符区间。例如,[abc] 将匹配"a", "b", 或
"c"中的任意一个字 符;也可以用区间[a-c]来表示同一字符集,和前者效果一致。如果你只想匹配小写 字母,那么 RE 应写成
[a-z],元字符在类别里并不起作用。例如,[akm" 中
的任意一个;"$"通常用作元字符,但在字符类别里,其特性被除去,恢复成普通字符。
三、Python正则表达式各种函数以及参数解析
match: re.match(pattern,string,flags=0)
flags 编译标志位,用于修改正则表达式的匹配方式,如:是否区别大小写
re.match("com","comwww.runcomoob").group()
'com'
re.match("com","Comwww.runComoob",re.I).group()
'Com'
flags 编译标志位
re.I 使匹配对大小写不敏感
>>> re.search("com","COM",re.I).group()
'COM'
re.L 做本地化识别(locale-aware)匹配
re.M 多行匹配,影响^和$
re.S 使.匹配包括换行在内的所有字符
>>> re.findall(".","abc\nde")
['a', 'b', 'c', 'd', 'e']
>>> re.findall(".","abc\nde",re.S)
['a', 'b', 'c', '\n', 'd', 'e']
re.U 根据Unicode字符集解析字符。这个标志影响 \w, \W, \b, \B
re.X 该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。
search:re.search(pattern,string,flags=0)
re.search("\dcom"," [ www.4comrunoob.5com").group
](http://www.4comrunoob.5com").group) ()
'4com'
re.match与re.search的区别
re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。
match和search一旦匹配成功,就是一个match object对象,而match object对象有以下方法:
- group() 返回被RE匹配的字符串
- start() 返回匹配开始的位置
- end() 返回匹配结束的位置
- span() 返回一个元组包含匹配(开始,结束)的位置
- group() 返回re整体匹配的字符串,可以一次输入多个组号,对应组号匹配的字符串,获取匹配到的所有结果(无论是否有组)
- •a. group () 返回re整体匹配的字符串,
- •b. group (n,m) 返回组号为n,m所匹配的字符串,如果组号不存在,则返回indexError异常
- •c. groups() groups() 方法返回一个包含正则表达式中所有小组字符串的元组,从 1到所含的小组号,通常groups()不需要参数,返回一个元组,元组中的元就是正则表达式中定义的组。
a = "123abc456"
re.search("([0-9])([a-z])([0-9])",a).group(0) #123abc456,返回整体
re.search("([0-9])([a-z])([0-9])",a).group(1) #123
re.search("([0-9])([a-z])([0-9])",a).group(2) #abc
re.search("([0-9])([a-z])([0-9])",a).group(3) #456
在上面的代码练习中,我们看到很多代码后面加有group,在这我们就针对这个group进行解析。
m = re.match("([abc])+", "abc")
一般,m.group(N) 返回第N组括号匹配的字符。 而m.group() == m.group(0) ==
所有匹配的字符,与括号无关,这个是API规定的。 m.groups() 返回所有括号匹配的字符,以tuple格式。m.groups() ==
(m.group(0), m.group(1), ...)
sub subn:
re.sub(pattern, repl, string, max=0)
>>> re.sub("g.t","have","I get A, I got B, I gut C")#匹配g.t字符,用have替换(.匹配一个除了换行符任意一个字符)
'I have A, I have B, I have C'
>>> re.sub("got","have","I get A, I got B, I gut C")
'I get A, I have B, I gut C'
>>> re.sub("g.t","have","I get A, I got B, I gut C",2)#替换两个
'I have A, I have B, I gut C'
>>> re.sub("g.t","have","I get A, I got B, I gut C",1)
'I have A, I got B, I gut C'
>>> re.subn("g.t","have","I get A, I got B, I gut C")#使用re.subn显示替换里多少个
('I have A, I have B, I have C', 3)
re.compile(strPattern[, flag]):
这个方法是Pattern类的工厂方法,用于将字符串形式的正则表达式编译为Pattern对象。
第二个参数flag是 匹配模式,取值可以使用按位或运算符‘|‘表示同时生效,比如re.I |re.M可以把正则表达式编译成一个正则表达式对象。可以把那些经常使用的正则表达式编译成正则表达式对象,这样可以提高一定的效率。
一个正则表达式对象的一个例子:
>>> text = "JGood is a handsome boy, he is cool, clever, and so on..."
>>> regex = re.compile(r"\w*oo\w*")
>>> print regex.findall(text)
['JGood', 'cool']
split:
p = re.compile(r"\d+") #+:匹配前面那个字符1到多次
p.split("one1two2three3four4") #spilt分割
>>> p = re.compile(r"\d+")
>>> p.split("one1two2three3four4")
['one', 'two', 'three', 'four', '']
re.split("\d+","one1two2three3four4")
>>> re.split("\d+","one1two2three3four4")
['one', 'two', 'three', 'four', '']
>>> re.split("\d+","4one1two2three3four4")
['', 'one', 'two', 'three', 'four', '']#如果分割时左边或者右边已经被分过
>>> re.split("[bc]","abcd")#或者是无字符的情况下,就分出一个空字符
['a', '', 'd']
finditer():
>>> p = re.compile(r"\d+")
>>> iterator = p.finditer("12 drumm44ers drumming, 11 ... 10 ...")
>>>
>>> iterator
<callable-iterator object at 0x02626990>
>>> for match in iterator:
... match.group() , match.span()#每个数字以及它们出现的位置
...
('12', (0, 2))
('44', (8, 10))
('11', (24, 26))
('10', (31, 33))
由于我们是在python下使用的正则表达式,所以特殊字符需要多次转意,而使用了rawstring之后,就不用在多次转意仅仅就使用正则的规则就可以。
>>> re.findall(r"\d","www4dd6")
['4', '6']
>>> re.findall("\\d","www4dd6")
['4', '6']
>>> re.findall("\d","www4dd6")
['4', '6']
#在这里\d成功的原因是因为\d在ascii码中没有特殊含义,所以在这里就自动转意了,不过正规的写法就是前两个
单词边界
>>> re.findall(r"\babc","abcsd abc")
['abc', 'abc']
>>> re.findall(r"abc\b","abcsd abc")
['abc']
>>> re.findall(r"abc\b","abcsd abc*")
['abc']
>>> re.findall(r"\babc","*abcsd*abc")
['abc', 'abc']
#检测单词边界不一定就是空格,还可以是除了字母以外的特殊字符
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)