一、rematch函数忽视大小写
在使用正则表达式匹配时,有时会遇到大小写不一致的情况。re.match函数提供了一个忽略大小写的参数,使得匹配更加灵活。
import re # 匹配单词cat,不区分大小写 string1 = "The Cat is under the table" pattern1 = re.compile(r"cat", re.IGNORECASE) result1 = pattern1.match(string1) print(result1.group()) # 输出Cat
在这个示例中,使用了re.IGNORECASE参数来忽略大小写。因此,可以成功匹配到字符串中的”Cat”,而不是”cat”。
二、remainder函数
在使用re.match函数匹配字符串时,常常会遇到一些不必要的字符需要剔除。此时可以使用remainder函数来去除match对象中匹配到的部分之外的部分。
import re # 匹配单词cat,不区分大小写 string2 = "The Cat is under the table" pattern2 = re.compile(r"cat", re.IGNORECASE) result2 = pattern2.match(string2).remainder print(result2) # 输出" is under the table"
在这个示例中,使用了remainder属性来获取匹配之后的剩余部分。因此,得到的结果是” is under the table”,即匹配到的”Cat”之后的部分。
三、rematch函数python什么意思
rematch函数是re.match函数的别名。这个函数的作用是匹配字符串开头的正则表达式。
import re # 匹配字符串开头的数字 string3 = "2022 is a new year" pattern3 = re.compile(r"^\d+") result3 = pattern3.match(string3) print(result3.group()) # 输出"2022"
在这个示例中,使用re.match函数匹配了字符串开头的数字。通过使用”^”符号,可以匹配字符串的开头。因此,得到的结果是”2022″。
四、rearrange函数
rearrange函数可以将一个正则表达式中的不同部分调换位置。这在一些复杂的匹配过程中非常有用。
import re # 匹配手机号码 string4 = "My phone number is 123-456-7890" pattern4 = re.compile(r"(\d{3})-(\d{3})-(\d{4})") result4 = pattern4.match(string4).rearrange(r"\3-\1-\2") print(result4) # 输出"7890-123-456"
在这个示例中,先使用pattern4.match函数匹配了字符串中的手机号码,然后使用rearrange函数将后三个数字与前三个数字调换位置。因此,得到的结果是”7890-123-456″。
五、replicate函数
replicate函数可以将正则表达式的某个部分进行重复,从而让匹配更加灵活。
import re # 匹配重复的单词 string5 = "the the cat is under the the table" pattern5 = re.compile(r"\b(\w+)\b\s+(?P\b\1\b)") result5 = pattern5.findall(string5) for match in result5: print(match.group("rep")) # 输出"the"
在这个示例中,使用了replicate函数将表达式中的“\1”处重复匹配前面的表达式。因此,能够匹配到连续出现的单词“the”,并输出其中的“the”。
六、remove函数
remove函数可以将match对象中的某个分组删除。
import re # 匹配姓名和学号 string6 = "姓名:张三,学号:20220001" pattern6 = re.compile(r"姓名:(?P\w+),学号:(?P\d+)") result6 = pattern6.match(string6) result6.remove("name") print(result6.group()) # 输出"学号:20220001"
在这个示例中,使用remove函数删除了match对象中的”name”分组。因此,得到的结果是”学号:20220001″。
七、rept函数
rept函数可以将正则表达式中某个部分进行重复,并指定重复的次数。
import re # 匹配连续的数字 string7 = "123 456 789 101112" pattern7 = re.compile(r"\d{3}") result7 = pattern7.findall(string7) result7 = pattern7.rept("(", ",", ")", result7) print(result7) # 输出"(123),(456),(789),(101),(112)"
在这个示例中,使用rept函数将表达式中的”\d{3}”处重复匹配3次,并在每次匹配之间添加”,”符号。然后使用括号将每个匹配了的数字括起来。因此,得到的结果是”(123),(456),(789),(101),(112)”。
八、remove函数python
remove函数是match对象的一个方法。其作用是删除match对象中的某个分组。
import re # 匹配身份证号码 string8 = "身份证号码:1234567890" pattern8 = re.compile(r"身份证号码:(?P\d+)") result8 = pattern8.match(string8) result8.remove("number") print(result8.group()) # 输出"身份证号码:"
在这个示例中,使用remove函数删除了match对象中的”number”分组。因此,得到的结果是”身份证号码:”。注意,如果删除了所有的分组,那么match对象将没有任何值。
九、replica函数
replica函数可以将正则表达式中的某个部分进行复制,并且指定复制的次数。
import re # 匹配重复的字符 string9 = "abccdeffgh" pattern9 = re.compile(r"(?P[a-z])\1{2}") result9 = pattern9.findall(string9) result9 = pattern9.replica("[", ":", "]", 2, result9) print(result9) # 输出"[[ccc],[ff]]"
在这个示例中,使用replica函数将表达式中的”(?P<alpha>[a-z])\1{2}”处重复匹配2遍,并使用中括号将结果括起来。因此,得到的结果是”[[ccc],[ff]]”。
十、rem函数与mod函数
rem函数和mod函数是match对象的两个方法,其作用分别是返回match对象中某个分组的内容和剩余部分的内容。
import re # 匹配电话号码 string10 = "电话号码:123-456-7890" pattern10 = re.compile(r"电话号码:(?P\d+-\d+-\d+)") result10 = pattern10.match(string10) num = result10.rem("number") area_code = result10.rem(1) print(num) # 输出"123-456-7890" print(area_code) # 输出"123"
在这个示例中,使用rem函数和mod函数分别获取了match对象中的电话号码和区号。其中,rem函数的参数可以是分组的名称或者分组的索引。因此,得到的结果分别是”123-456-7890″和”123″。
原创文章,作者:PBKV,如若转载,请注明出处:https://www.506064.com/n/145218.html