Python正则表达式实例详解(3)

search(正则表达式, 字符串)
--> 查找字符串中满足正则表达式的第一个字符串。返回值是匹配对象或者None

result = re.search(r'(\d)[a-zA-Z]+', 'uhsh2hdje+984nf') print(result.group(0)) print(result.group(1)) print(result.string) 练习:使用search匹配出一个字符串中所有的数字字符串'abc34jshd8923jkshd9lkkk890k' --> 34,8923,9,890 re_str = r'\d+' str1 = 'abc34jshd8923jkshd9lkkk890k' result = re.search(re_str, str1) while result: print(result) str1 = str1[result.end():] result = re.search(re_str, str1) 4.findall

findall(正则表达式, 字符串) --> 获取字符串中满足正则表达式的所有的子串,返回一个列表
注意:如果正在表达式中有分组,取值的时候只取分组中匹配到的结果;
如果有多个分组,会将每个分组匹配到的结果作为一个元祖的元素

re_str = r'(\d+)k([a-d]+)' str1 = 'abc34kshd8923kabcshd9lkkk890kaa' result = re.findall(re_str, str1) print(result) # [('8923', 'abc'), ('890', 'aa')] re_str = r'(\d+)k[a-d]+' str1 = 'abc34kshd8923kabcshd9lkkk890kaa' result = re.findall(re_str, str1) print(result) # ['8923', '890'] re_str = r'\d+k[a-d]+' str1 = 'abc34kshd8923kabcshd9lkkk890kaa' result = re.findall(re_str, str1) print(result) # ['8923kabc', '890kaa'] 5.finditer

finditer(正则表达式, 字符串)
--> 查找所有满足正则条件的子串,返回值是迭代器,迭代器中的元素是匹配对象

re_str = r'\d+' str1 = 'abc34kshd8923kabcshd9lkkk890kaa' result = re.finditer(re_str, str1) print(result) for item in result: print(item) 6. split

split(正则表达式,字符串) --> 将字符串按照满足正则表达式条件的子串进行分割
"""
str1 = 'ahsb1sssa8-jjhd7nhs+90nsjhf3-4hhh7+8kjj-'
result = re.split(r'[-+]', str1)
print(result)

7.sub

sub(正则表达式,repl,字符串) --> 将字符串中满足正则表达式条件的子串替换成repl。返回替换后的字符串

str1 = 'hsj8jskfh98ssjj8hshh' result = re.sub(r'\d+','*', str1) print(result) str1 = '智 障,你在干嘛?逼, 后视镜,妈的加扣扣上' result = re.sub(r'傻\s*叉|逼|fuck|妈的|智\s*障', '*', str1) print(result) with open('./data', 'r', encoding='utf-8') as f: content = f.read() # "name": "搞笑精选汇" result = re.findall(r'"name":"(.+?)"', content) print(result) 作业 1. 写一个正则表达式判断一个字符串是否是ip地址

规则:一个ip地址由4个数字组成,每个数字之间用.连接。每个数字的大小是0-255 例如:255.189.10.37 正确 256.189.89.9 错误

import re re_str = r'((\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])' result = re.fullmatch(re_str,'255.183.10.37') print(result) 2. 计算一个字符串中所有的数字的和

例如:字符串是:‘hello90abc 78sjh12.5’ 结果是90+78+12.5 = 180.5

str1 = 'hello90abc 78sjh12.5' result = re.findall(r'[^a-z]+',str1) print(result) sum1 = 0 for item in result: sum1 += float(item) print(sum1) 3. 验证输入的内容只能是汉字 re_str = r'[\u4E00-\u9fa5]+' result = re.fullmatch(re_str,'你好') print(result) 4. 电话号码的验证 re_str = r'(13\d|150[0-3]|15[5-9]|17[6-8]|18[0-9])\d{8}' result= re.fullmatch(re_str,'15589154445') print(result) 5. 简单的身份证号的验证 re_str=r'^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$' result = re.fullmatch(re_str,'510211199505181572') print(result) 二、不定项选择题

能够完全匹配字符串“(010)-62661617”和字符串“01062661617”的正则表达式包括( A B D )
A. “(?\d{3})?-?\d{8}”
B. “[0-9()-]+”
C. “[0-9(-)]\d”
D. “[(]?\d[)-]\d*”

能够完全匹配字符串“c:\rapidminer\lib\plugs”的正则表达式包括( B C )
A. “c:\rapidminer\lib\plugs”
B. “c:\rapidminer\lib\plugs”
C. “(?i)C:\RapidMiner\Lib\Plugs” ?i:将后面的内容的大写变成小写
D. “(?s)C:\RapidMiner\Lib\Plugs” ?s:单行匹配

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/b28b6aceda5668de3729d2fc793c76b1.html