find | 您所在的位置:网站首页 › findall方法返回值为什么有一个空字符串 › find |
![]() 正则 re.findall 的简单用法(返回string中所有与pattern相匹配的全部字串,返回形式为数组)语法: findall(pattern, string, flags=0)Python 正则表达式 re findall 方法能够以列表的形式返回能匹配的子串 总共有7种标识: r 标识 符号 ^ 符号 $ 符号[ ] d 标识 大 D标识 w 标识 大 W 标识 r 标识 :查找全部 r 标识代表后面是正则的语句 import re str1 = re.findall(r"zhihu","https://zhuanlan.zhihu.com/c_1137363974214598656") print (str1)输出: ['zhihu'] 符号 ^ :表示匹配以https开头的的字符串返回 import re str2 = re.findall(r"^https","https://zhuanlan.zhihu.com/c_1137363974214598656") print (str2)输出: ['https'] 符号 $ :表示以56结尾的字符串返回,判断是否字符串结束的字符串 import re str3 = re.findall(r"56$","https://zhuanlan.zhihu.com/c_1137363974214598656") print (str3)输出: ['56'] 符号 [ ] :匹配括号中的其中一个字符 import re str4 = re.findall(r"[d,c]o","https://zhuanlan.zhihu.com/c_1137363974214598656") print (str4)输出: ['co'] d 标识 :是用来匹配0到9之间的数返回列表 str5 = re.findall(r"d","https://zhuanlan.zhihu.com/c_986") str6 = re.findall(r"dddd","3736https://zhuanlan.zhihu.com/c_656111") print (str5) print (str6)输出: ['9', '8', '6'] ['3736', '6561'] D 标识 :是用来匹配数字之外的值返回列表 import re str7 = re.findall(r"DD","https://zhuanlan.zhihu.com/c_1137363974214598656") print (str7)输出: ['ht', 'tp', 's:', '//', 'zh', 'ua', 'nl', 'an', '.z', 'hi', 'hu', '.c', 'om', '/c'] w 标识 :匹配从小写a到z,大写A到Z,数字0到9 import re str8 = re.findall(r"w","https://zhuanlan.zhihu.com/110") print (str8)输出: ['h', 't', 't', 'p', 's', 'z', 'h', 'u', 'a', 'n', 'l', 'a', 'n', 'z', 'h', 'i', 'h', 'u', 'c', 'o', 'm', '1', '1', '0'] 大 W 标识 :匹配除了字母与数字以外的特殊符号 import re str9 = re.findall(r"DD","https://zhuanlan.zhihu.com/c_1137363974214598656") print (str9)输出: [':', '/', '/', '.', '.', '/'] |
CopyRight 2018-2019 实验室设备网 版权所有 |