import re
reg_string = "hello9527python@wangcai.@!:xiaoqiang"
reg = "hello"
result = re.findall(reg, reg_string)
print(result)
元字符
import re
'''
. 匹配换行符之外的任意字符
\w 匹配字母或数组或汉字
\s 匹配任意的空白符
\d 匹配数字
\b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束
'''
reg_string = "hello9527python@小强hello"
result = re.findall('\d', reg_string)
print(result)
result = re.findall('^hello', reg_string)
print(result)
result = re.findall('\w', reg_string)
print(result)
反义代码
import re
'''
. 匹配换行符之外的任意字符
\W 匹配任意不是字母、数组、汉字的字符
\S 匹配任意不是空白的字符
\D 匹配非数字
\B 匹配不是单词的开始或结束的位置
[^a] 匹配除了a以外的任意字符
'''
reg_string = "hello9527python@小强hello"
result = re.findall('\D', reg_string)
print(result)
result = re.findall('[^h]', reg_string)
print(result)
result = re.findall('\W', reg_string)
print(result)
限定符
import re
'''
* 重复零次或多次
+ 重复一次或多次
? 重复零次或一次
{n} 重复 n 次
{n, } 重复 n 次或更多次
{n, m} 重复 n 到 m 次
'''
reg_string = "hello9527python@小强hello"
result = re.findall('\d{4}', reg_string)
print(result)
result = re.findall('[0-9a-z]{4}', reg_string)
print(result)
练习
import re
ip = "this is ip:192.168.2.123 : 172.138.2.15"
result = re.findall("\d{3}.\d+.\d+.\d+", ip)
print(result)
'''
search 只匹配第一个
findall 匹配所有
'''
result = re.search("(\d{1,3}.){3}\d{1,3}", ip)[0]
print(result)
组匹配
import re
s = "this is phone:13888888888 and this is my postcode:012345"
reg = "this is phone:(\d{11}) and this is my postcode:(\d{6})"
result = re.search(reg, s).group(0)
print(result)
result = re.search(reg, s).group(1)
print(result)
result = re.search(reg, s).group(2)
print(result)
# match 只匹配开头
reg_string = "HelloPythonString"
result = re.match("Hello", reg_string).group()
print(result)
# re.I 忽略大小写
reg_string = "helloPythonString"
result = re.match("Hello", reg_string, re.I).group()
print(result)
贪婪与懒惰
import re
'''
贪婪:尽可能多的匹配
懒惰:尽可能少的匹配
懒惰操作符:?,位于 * + 后面,要求正则匹配越少越好
* 重复零次或多次
+ 重复一次或多次
? 重复零次或一次
'''
reg_string = "pythonnnnnnnnnpythonHelloPython"
# 贪婪
result = re.findall("python*", reg_string)
print(result)
# 懒惰
result = re.findall("python*?", reg_string)
print(result)
result = re.findall("python+?", reg_string)
print(result)
result = re.findall("python??", reg_string)
print(result)