-s 数字 没有 ret int bcs 意义 sdl
re:
本质上就是一种小型语言。
操作的对象是字符串。
re.findall(): 返回的是一个列表。匹配出所有符合条件的元素。
re.search(): 返回的是一个对象。只匹配出找到的第一个元素, 如果匹配成功那么返回的对象就是含匹配目标的索引范围, 和匹配内容。匹配失败就返回 None。
re.match(): 返回跟 search 一样也是一个对象, 只匹配文件开头位置。如果匹配成功那么返回的对象就是含匹配目标的索引范围, 和匹配内容。匹配失败就返回 None。
- 示例:
- s ='abclaskdjaklsjdlasdjabcasd123lksdlasd0asdasdaaa'
- res = re.match('abc',s)
- print(res,res.group())
- res = re.search('aaa',s)
- print(res,res.group())
- res = re.findall('abc',s)
- print(res)
- 执行结果:
- D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_练习.py
- <_sre.SRE_Match object; span=(0, 3), match='abc'> abc
- <_sre.SRE_Match object; span=(44, 47), match='aaa'> aaa
- ['abc','abc']
- Process finished with exit code 0
元字符:
.: 可以指代除换行符以外的任意符号。
- import re
- s ='abclaskdjaklabcsjdlasdjaba1ccasd123a/clksdlasd0asdasdaaa'
- res = re.findall('a.c',s)
- print(res)
- res = re.findall('a..c',s)
- print(res)
- 执行结果:
- D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_练习.py
- ['abc','abc','a1c','a/c']
- ['a1cc']
- Process finished with exit code 0
^: 只匹配字符串的开头。
- import re
- s ='abc1231321abclskdjlsk654sdsd3a13213a'
- s1 ='32132abc'
- res = re.findall('^abc',s)
- print(res)
- res = re.findall('^abc',s1)
- print(res)
- 执行结果:
- D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_练习.py
- ['abc']
- []
- Process finished with exit code 0
$: 只匹配字符串的结尾。
- import re
- s ='abc1231321abclskdjlsk654sdsd3a13213a'
- s1 ='32133abc'
- res = re.findall('3a$',s)
- print(res)
- res = re.findall('3a$',s1)
- print(res)
- 执行结果:
- D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_练习.py
- ['3a']
- []
- Process finished with exit code 0
*: 符号前一个字符重复 0 - 无穷次。
- import re
- s ='abccccccabccdllkjabc'
- res = re.findall('abc*',s)
- print(res)
- 执行结果:
- D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_练习.py
- ['abcccccc','abcc','abc']
- Process finished with exit code 0
+: 符号前一个字符重复 1 - 无穷次。
- import re
- s ='abccccccabccdllkjabcabababc'
- res = re.findall('abc+',s)
- print(res)
- 执行结果:
- D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_练习.py
- ['abcccccc','abcc','abc','abc']
- Process finished with exit code 0
: 符号前一个字符重复 0-1 次。
- import re
- s ='abccccccabccdllkjabcabababc'
- res = re.findall('abc?',s)
- print(res)
- 执行结果:
- D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_练习.py
- ['abc','abc','abc','ab','ab','abc']
- Process finished with exit code 0
{}: 符号前一个字符指定重复的次数。
- import re
- s ='abccccccabccdllkjabcccabababc'
- res = re.findall('abc{3,4}',s)
- print(res)
- res = re.findall('abc{5}',s)
- print(res)
- 执行结果:
- D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_练习.py
- ['abcccc','abccc']
- ['abccccc']
- Process finished with exit code 0
\: 转义符, 转换后面字符的原本意思。
转义符跟元字符在一起的时候会去除其功能。还可以跟特殊的普通符号, 把某些普通的符号转成特别的意义。
可转换的普通符号:
- \d 匹配任何十进制数; 它相当于类 [0-9]
- \D 匹配任何非数字字符; 它相当于类 [^0-9]
- \s 匹配任何空白字符; 它相当于类 [ \t\n\r\f\v]
- \S 匹配任何非空白字符; 它相当于类 [^ \t\n\r\f\v]
- \w 匹配任何字母数字字符; 它相当于类 [a-zA-Z0-9_]
- \W 匹配任何非字母数字字符; 它相当于类 [^a-zA-Z0-9_]
- \b 匹配一个特殊字符边界,比如空格 ,&,#等
(): 分组。把某些内容固定成一项。
- import re
- s ='rabhdg8s99d222a1'
- ret=re.findall('(ab)|\d+',s)
- print(ret)
- 执行结果:
- D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_练习.py
- ['ab','','','','']
- Process finished with exit code 0
- ():还有个优先显示功能,所以上面数字没有显示出来。
- 通过下列办法解决:
- import re
- s ='rabhdg8s99d222a1'
- ret=re.findall('(?:ab)|\d+',s)
- print(ret)
- 执行结果:
- D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_练习.py
- ['ab','8','99','222','1']
- Process finished with exit code 0
有名分组:
- import re
- s ='rabhdg8s99d222a1'
- ret=re.search('(?P<test>ab)',s)
- print(ret.group('test'))
- 执行结果:
- D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_练习.py
- ab
- Process finished with exit code 0
[]: 字符集 (多选一) 只匹配一个字符集中的一个字符。
在字符集中 只有 ^ \ - 还有自己的特殊意义, 其他的都是普通的字符集, 在字符集中的 ^ 号不是代表匹配开头而是取反的意思。
- import re
- s ='sdnaskdabcaspdlabefla1231ksaaabbcpas213lsdcbcablkc'
- res = re.findall('ab[ce]',s)
- print(res)
- res = re.findall('[a-z]+',s)
- print(res)
- res = re.findall('[0-9]+',s)
- print(res)
- res = re.findall('[^a-z]+',s)
- print(res)
- res = re.findall('[\d]+',s)
- print(res)
- 执行结果:
- D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_练习.py
- ['abc','abe']
- ['sdnaskdabcaspdlabefla','ksaaabbcpas','lsdcbcablkc']
- ['1231','213']
- ['1231','213']
- ['1231','213']
- Process finished with exit code 0
|: 或的意思。
- import re
- s ='sdnaskdabcaspdlabefla1231ksaaabbcpas213lsdcbcablkc'
- res = re.findall('a.c|\d+',s)
- print(res)
- 执行结果:
- D:\Python\Python36-32\python.exe E:/Python/DAY-14/day14_练习.py
- ['abc','1231','213']
- Process finished with exit code 0
Python 基础 day-13[模块:re 未完]
来源: http://www.bubuko.com/infodetail-2139579.html