什么是正则表达式正则表达式regular expression常简写为regex、regexp或re是一种用于匹配和操作文本的强大工具它是由一系列字符和特殊字符组成的模式用于描述要匹配的文本模式。正则表达式可以在文本中查找、替换、提取和验证特定的模式。re模块Python的re模块提供了正则表达式匹配操作。importrere模块中提供了一些方法用于查找或处理字符串。searchre.search(pattern,string)扫描整个 string 查找正则表达式 pattern 产生匹配的第一个位置并返回相应的 Match。如果字符串中没有与模式匹配的位置则返回 None。matchre.match(pattern,string)如果 string 开头的零个或多个字符与正则表达式 pattern 匹配则返回相应的 Match。如果字符串与模式不匹配则返回 None。findallre.findall(pattern,string)返回 pattern 在 string 中的所有非重叠匹配以字符串列表或字符串元组列表的形式。对 string 的扫描从左至右匹配结果按照找到的顺序返回。空匹配也包括在结果中。subre.sub(pattern,repl,string,count0)返回通过使用 repl 替换在 string 最左边非重叠出现的 pattern 而获得的字符串。如果样式没有找到则不加改变地返回 string。repl 可以是字符串或函数如为字符串则其中任何反斜杠转义序列都会被处理。 也就是说\n 会被转换为一个换行符\r 会被转换为一个回车符依此类推。如果 repl 是一个函数则它会针对每次 pattern 的非重叠出现的情况被调用。 该函数接受单个 Match 参数并返回替换字符串。可选参数 count 是要替换的最大次数count 必须是非负整数。如果省略这个参数或设为 0所有的匹配都会被替换。splitre.split(pattern,string,maxsplit0)用 pattern 分开 string 。 如果在 pattern 中捕获到括号那么所有的组里的文字也会包含在列表里。如果 maxsplit 非零 最多进行 maxsplit 次分隔 剩下的字符全部返回到列表的最后一个元素。表示字符表示数量表示边界匹配分组注意. ^ $ * ? { } [ ] \ | ( )属于元字符[ 和 ] 。这两个元字符用于指定一个字符类也就是你希望匹配的字符的一个集合。元字符 (除了 ) 在字符类中是不起作用的。 例如[akm] 将会匹配以下任一字 符 ′ a ′ , ′ k ′ , ′ m ′ 或 ′ ] 将会匹配以下任一字符 a, k, m 或 ]将会匹配以下任一字符′a′,′k′,′m′或′‘’$’ 通常是一个元字符但在一个字符类中它的特殊性被消除了。原始字符串Python中字符串前面加上 r 表示原始字符串忽略转义。原始字符串非常适合用于正则表达式因为正则表达式中通常包含很多反斜杠例如 \d 或 \w使用原始字符串可以避免反斜杠带来的转义问题。例如importre textabcdef123456print(re.search(r\w,text))print(re.search(\w,text))# SyntaxWarning: invalid escape sequence \w不使用原始字符串虽然也能运行但是会有语法警告。案例匹配电话号码importre test[13812345678,# 合法11456817239,# 非法19912345678,# 合法17138412356,# 合法1234567890,# 非法14752345673,# 合法1800123456,# 非法]# 以1开头第二位为345789后面是9位数字patternr^1[345789]\d{9}$foriintest:print(f{i:20}{合法 if re.match(pattern, i) else 非法})匹配邮箱importre test[exampleexample.com,user.namesubdomain.example.co,username.com,missingusername.com,-dasdqq.com,]# 匹配邮箱patternr[\w!#$%*-/?^{|}~.][\w!#$%*-/?^{|}~.]\.[a-zA-Z]{2,}$foriintest:print(f{i:40}{合法 if re.match(pattern, i) else 非法})匹配0-255之间的数字importre test[0,9,50,100,199,200,255,256,-1,01,001]# 十位为1-9?表示可以没有十位个位是0-9# 或 百位是1十位是0-9个位是0-9# 或 百位是2十位是0-4个位是0-9# 或 百位是2十位是5个位是0-5patternr^([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])$fornumintest:print(f{num:5}{合法 if re.match(pattern, num) else 非法})从标签中获取网址importre testlink relalternate hreflangzh hrefhttps://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hans hrefhttps://zh.wikipedia.org/zh-hans/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hans-CN hrefhttps://zh.wikipedia.org/zh-cn/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hans-MY hrefhttps://zh.wikipedia.org/zh-my/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hans-SG hrefhttps://zh.wikipedia.org/zh-sg/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hant hrefhttps://zh.wikipedia.org/zh-hant/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hant-HK hrefhttps://zh.wikipedia.org/zh-hk/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hant-MO hrefhttps://zh.wikipedia.org/zh-mo/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hant-TW hrefhttps://zh.wikipedia.org/zh-tw/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangx-default hrefhttps://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F# 获取所有href中网址patternrhref\(.?)\foriinre.findall(pattern,test):print(i)替换文本中的所有数字为对应的词importre testI have 2 apples and 3 oranges.# 定义数字到词的映射num_map{1:one,2:two,3:three,4:four,5:five}print(re.sub(r\d,lambdax:num_map[x.group(0)],test))# I have two apples and three oranges.
16_正则表达式
什么是正则表达式正则表达式regular expression常简写为regex、regexp或re是一种用于匹配和操作文本的强大工具它是由一系列字符和特殊字符组成的模式用于描述要匹配的文本模式。正则表达式可以在文本中查找、替换、提取和验证特定的模式。re模块Python的re模块提供了正则表达式匹配操作。importrere模块中提供了一些方法用于查找或处理字符串。searchre.search(pattern,string)扫描整个 string 查找正则表达式 pattern 产生匹配的第一个位置并返回相应的 Match。如果字符串中没有与模式匹配的位置则返回 None。matchre.match(pattern,string)如果 string 开头的零个或多个字符与正则表达式 pattern 匹配则返回相应的 Match。如果字符串与模式不匹配则返回 None。findallre.findall(pattern,string)返回 pattern 在 string 中的所有非重叠匹配以字符串列表或字符串元组列表的形式。对 string 的扫描从左至右匹配结果按照找到的顺序返回。空匹配也包括在结果中。subre.sub(pattern,repl,string,count0)返回通过使用 repl 替换在 string 最左边非重叠出现的 pattern 而获得的字符串。如果样式没有找到则不加改变地返回 string。repl 可以是字符串或函数如为字符串则其中任何反斜杠转义序列都会被处理。 也就是说\n 会被转换为一个换行符\r 会被转换为一个回车符依此类推。如果 repl 是一个函数则它会针对每次 pattern 的非重叠出现的情况被调用。 该函数接受单个 Match 参数并返回替换字符串。可选参数 count 是要替换的最大次数count 必须是非负整数。如果省略这个参数或设为 0所有的匹配都会被替换。splitre.split(pattern,string,maxsplit0)用 pattern 分开 string 。 如果在 pattern 中捕获到括号那么所有的组里的文字也会包含在列表里。如果 maxsplit 非零 最多进行 maxsplit 次分隔 剩下的字符全部返回到列表的最后一个元素。表示字符表示数量表示边界匹配分组注意. ^ $ * ? { } [ ] \ | ( )属于元字符[ 和 ] 。这两个元字符用于指定一个字符类也就是你希望匹配的字符的一个集合。元字符 (除了 ) 在字符类中是不起作用的。 例如[akm] 将会匹配以下任一字 符 ′ a ′ , ′ k ′ , ′ m ′ 或 ′ ] 将会匹配以下任一字符 a, k, m 或 ]将会匹配以下任一字符′a′,′k′,′m′或′‘’$’ 通常是一个元字符但在一个字符类中它的特殊性被消除了。原始字符串Python中字符串前面加上 r 表示原始字符串忽略转义。原始字符串非常适合用于正则表达式因为正则表达式中通常包含很多反斜杠例如 \d 或 \w使用原始字符串可以避免反斜杠带来的转义问题。例如importre textabcdef123456print(re.search(r\w,text))print(re.search(\w,text))# SyntaxWarning: invalid escape sequence \w不使用原始字符串虽然也能运行但是会有语法警告。案例匹配电话号码importre test[13812345678,# 合法11456817239,# 非法19912345678,# 合法17138412356,# 合法1234567890,# 非法14752345673,# 合法1800123456,# 非法]# 以1开头第二位为345789后面是9位数字patternr^1[345789]\d{9}$foriintest:print(f{i:20}{合法 if re.match(pattern, i) else 非法})匹配邮箱importre test[exampleexample.com,user.namesubdomain.example.co,username.com,missingusername.com,-dasdqq.com,]# 匹配邮箱patternr[\w!#$%*-/?^{|}~.][\w!#$%*-/?^{|}~.]\.[a-zA-Z]{2,}$foriintest:print(f{i:40}{合法 if re.match(pattern, i) else 非法})匹配0-255之间的数字importre test[0,9,50,100,199,200,255,256,-1,01,001]# 十位为1-9?表示可以没有十位个位是0-9# 或 百位是1十位是0-9个位是0-9# 或 百位是2十位是0-4个位是0-9# 或 百位是2十位是5个位是0-5patternr^([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])$fornumintest:print(f{num:5}{合法 if re.match(pattern, num) else 非法})从标签中获取网址importre testlink relalternate hreflangzh hrefhttps://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hans hrefhttps://zh.wikipedia.org/zh-hans/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hans-CN hrefhttps://zh.wikipedia.org/zh-cn/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hans-MY hrefhttps://zh.wikipedia.org/zh-my/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hans-SG hrefhttps://zh.wikipedia.org/zh-sg/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hant hrefhttps://zh.wikipedia.org/zh-hant/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hant-HK hrefhttps://zh.wikipedia.org/zh-hk/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hant-MO hrefhttps://zh.wikipedia.org/zh-mo/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangzh-Hant-TW hrefhttps://zh.wikipedia.org/zh-tw/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F link relalternate hreflangx-default hrefhttps://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F# 获取所有href中网址patternrhref\(.?)\foriinre.findall(pattern,test):print(i)替换文本中的所有数字为对应的词importre testI have 2 apples and 3 oranges.# 定义数字到词的映射num_map{1:one,2:two,3:three,4:four,5:five}print(re.sub(r\d,lambdax:num_map[x.group(0)],test))# I have two apples and three oranges.