正则:它本身是很多语言都有的,本身是一门语言,根据字符串匹配寻找的一门工具,python通过接口调用方式,使用re模块
普通字符
import re
re.findall ('gg','fdsfdsggcvcssgg') 返回列表的格式
应该输出结果:['gg','gg']
===========================
'abcd'.find('b') 输出 1 ----查找这个字母在字符串中的位置
'abcd'.find('bc') 输出 1
'abcd'.split(b) 输出 ['a','cd'] ----分割
‘abcd’.replace('ab','ee') ---替换 输出 'eecd'
元字符 . ^ $ * + ? {} [] | ()
re.findall ('gg.v','fdsfdsggcvcssgg') ----输出 ['ggcv']
re.findall ('^gg','fdsfdsggcvcss') ----^ 控制开头,在开始满足才能匹配,这样就无法匹配
re.findall ('^gg','ggfdsfdsggcvcss') ---输出['gg'] 只有在字符串首才能配置上
re.findall ('gg$','fdsfdsggcvcssgg') ---- $ 控制末尾 ,输出['gg']
re.findall ('sg*','fdsfdsgggcvcss') ---- * 重复多个*号之前的字母,后面有多少都可以匹配
re.findall ('sg+','fdsfdsggggggg') ----—+ 一到多次 输出[sggggggg]
re.findall ('sg+','fdsfds') ------- 因为 + 号前面是g ,这样就无法匹配了
re.findall ('sg?','fdsfdsg') ------- ? 实现零次到一次 输出['sg']
re.findall ('sg?','fdsfdsgggg') ------- ? 实现零次到一次 输出['sg']
{} 功能最大,区间内的都可以匹配到
[] 或者的作用,其中的任意字符可以和外面匹配,
re.findall ('s[bc]g','wwsbg') ----输出 ['sbg]
re.findall ('s[bc]g','wwscg') ----输出 ['scg']
re.findall ('s[bc]g','wwsbcg') ----输出 [] 这样不能匹配,因为 [] 是或者的意思,不是并列 ,元字符放在字符集中就失去意义了,变成普通字符
re.findall ('[^1-9]','w31bc8') ---加上^是非的意思,输出:['w','b','c']