Python Regular Expressions
A quick reference to Python regular expressions, covering match counts, common syntax, pattern objects, searching, matching, splitting, finding, replacing, anchors, and use in web crawlers.
Read in Chinese→Python Regular Expressions
Quick Reference
import sys
import re
reload(sys)
sys.setdefaultencoding('utf-8')
Match Counts
. 匹配任意字符一次
* 匹配前面的表达式0/n次
+ 匹配前面的表达式1/n次
? 匹配前面的表达式0/1次
{m} 匹配前面的表达式m次
{m,n} 匹配前面的表达式m到n次
*? 匹配前面的表达式0次
+? 匹配前面的表达式1次
Common Syntax
re.S 匹配所有的字符
re.I 匹配大小写
re.M 多行匹配
re.match从头开始匹配
re.search全文匹配
\d 匹配所有的数字
\D 匹配所有的非数字
\s 匹配所有的文字
\S 匹配所有的非文本
[] 字符集合,匹配里面的所有表达式,使用|分开
Common Operations
Creating a Regular Expression Object
compile(pattern[, flags])
#Create a pattern object from a string containing a regular expression
Search the Entire Text for Objects Matching the Expression
search(pattern, string[, flags])
#在字符串中查找,只查找一个
Search for Objects Matching the Expression from the Beginning
match(pattern, string[, flags])
#在字符串的开始处匹配模式
Split a String
split(pattern, string[, maxsplit=0,flags])
#根据模式的匹配项来分割字符串
Find All Objects Matching the Expression
findall(pattern, string,flags)
#列出字符串中模式的所有匹配项
return 数组
Replace a String
sub(pat,repl, string[,count=0,flags])
#将字符串中所有的pat的匹配项用repl替换
Note: this denotes the end of a string, so adding it denotes the end of a string; therefore, adding it denotes the end of a string, meaning that the matched content must occur at the end of the string.
To match the entire string, also add the start-of-string marker ^
When writing a web crawler, you can use a regular expression to match multiple objects, then use a yield generator while iterating over them. The results can be combined into a dictionary, converted to JSON format with json.dumps(), and finally saved to a local text file.
Regular Expression Syntax for Standard Matching
汉字:[\u4e00-\u9fa5]{0,}
英文和数字:[A-Za-z0-9]+
长度为3-20的所有字符:.{3,20}
Email地址:\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)*
域名:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?
InternetURL:[a-zA-z]+://[^\s] 或 ^http://([\w-]+.)+[\w-]+(/[\w-./?%&=])?$
手机号码:^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$
国内电话号码:\d{3}-\d{8}|\d{4}-\d{7}(0511-4405222、021-87888822)
日期格式:^\d{4}-\d{1,2}-\d{1,2}
空白行的正则表达式:\n\s*\r (可以用来删除空白行)
腾讯QQ号:[1-9][0-9]{4,}
IP地址提取:\d+.\d+.\d+.\d+