-
-
Save stoensin/f319e7ac15268b80bff33b0799ce792c to your computer and use it in GitHub Desktop.
. 匹配任意字符 ^ 匹配字符串开始位置 $ 匹配字符串中结束的位置 * 前面的原子重复0次、1次、多次 ? 前面的原子重复0次或者1次 + 前面的原子重复1次或多次 {n} 前面的原子出现了 n 次 {n,} 前面的原子至少出现 n 次 {n,m} 前面的原子出现次数介于 n-m 之间 ( ) 分组,需要输出的部分
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| \s 匹配空白字符 | |
| \w 匹配任意字母/数字/下划线 | |
| \W 和小写 w 相反,匹配任意字母/数字/下划线以外的字符 | |
| \d 匹配十进制数字 | |
| \D 匹配除了十进制数以外的值 | |
| [0-9] 匹配一个0-9之间的数字 | |
| [a-z] 匹配小写英文字母 | |
| [A-Z] 匹配大写英文字母 | |
| abc=re.findall('[^\u4e00-\u9fa5]',str) #非汉字部分 | |
| zh_cn=re.findall('[\u4e00-\u9fa5]',str) #匹配汉字 | |
| reg = "1[3|4|5|7|8][0-9]{9}" | |
| phone_check= re.findall(reg, "1751208300") | |
| mobile_check= re.match(r'1[3,4,5,7,8]\d{9}$',"17512080300") | |
| s = 'This module provides regular expression matching operations similar to those found in Perl' | |
| word_pat = r'\s?([a-zA-Z]+)' | |
| r = re.findall(word_pat,s) | |
| # pat = r'\s+' | |
| # r = re.split(pat,s) | |
| print(r) # ['This', 'module', 'provides', 'regular', 'expression', 'matching', 'operations', 'similar', 'to', 'those', 'found', 'in', 'Perl'] | |
| content="hello 12345, hello 456321" | |
| pat=re.compile(r'\d+') #要替换的部分 | |
| m=pat.sub("666",content) | |
| print(m) # hello 666, hello 666 | |
| content='<h>ddedadsad</h><div>graph</div>bb<div>math</div>cc' | |
| pat=re.compile(r"<div>(.*?)</div>") | |
| m=pat.findall(content) | |
| print(m) # ['graph', 'math'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment