Contents

数据挖掘全流程包括采集、清洗和分析。而清洗工作往往会占到数据挖掘的80%的工作量。比如,最常见的文本数据,计算机是无法分析文本数据的,我们需要从文本中抽取出需要的数据,并将其编码为数字。这个从文本中抽取指定信息,往往需要用到正则表达式。
正则表达式语法
本小节语法比较难,大家看不懂也没关系。只需要保存该表,事后勤翻看即可。大家可以跳过此图,直接看后面的实际代码例子。
常用数字表达式


[0-9]+
匹配出字符串中0-9 中的任意数字信息,该信息满足数字出现一次或者多次
1 2 3 4 5 6 7 8 9 10 |
import re test = "hello 2019's world" #匹配出test中数字 pattern = '[0-9]+' re.findall(pattern, test) ['2019'] |
d{n}
匹配出字符串中n位的数字。n是数字出现次数,例子中我们设置为4
1 2 3 4 5 6 7 8 |
test = "hello 2019's world" #匹配出test中数字 pattern = 'd{4}' re.findall(pattern, test) ['2019'] |
d{n,}
匹配出字符串中整数至少出现n次
1 2 3 4 5 6 7 8 |
test = "hello 2019's world" #将d{n,}中的n改成0,1,2,3,4分别试试运行结果 pattern = 'd{4,}' re.findall(pattern, test) ['2019'] |
d{m,n}
匹配出字符串中的数字信息,该信息满足整数出现m-n次。
1 2 3 4 5 6 7 8 |
test = "hello 2019's world" #整数出现2-5次,也可以是1-7次。只要m<4, n>4即可 pattern = 'd{2,5}' re.findall(pattern, test) ['2019'] |
校验字符的表达式

[\u4e00-\u9fa5]+
匹配出字符串中的汉字
1 2 3 4 5 6 7 8 |
test = "亲们,让我们在2018一起学习Python_Language" #匹配出test中的中文 pattern = '[\u4e00-\u9fa5]+' re.findall(pattern, test) ['亲们', '让我们在', '一起学习'] |
[A-Za-z0-9]+
匹配出字符串中的英文和数字
1 2 3 4 5 6 7 8 |
test = "亲们,让我们在2018一起学习Python_Language" #匹配出test中的英文和数字 pattern = '[A-Za-z0-9]+' re.findall(pattern, test) ['2018', 'Python', 'Language'] |
.{m,n}
对某字符串进行匹配,匹配出m-n长度的所有字符串
1 2 3 4 5 6 7 8 |
test = "亲们,让我们在2018一起学习Python_Language" #例子,匹配出test中长度在1-4所有的字符串 pattern = '.{1,4}' re.findall(pattern, test) ['亲们,让', '我们在2', '018一', '起学习P', 'ytho', 'n_La', 'ngua', 'ge'] |
[A-Za-z]+
匹配出字符串中的英文字符,不考虑大小写
1 2 3 4 5 6 7 8 |
test = "亲们,让我们在2018一起学习Python_Language" #匹配出test中的英文字符,不考虑大小写 pattern = '[A-Za-z]+' re.findall(pattern, test) ['Python', 'Language'] |
[A-Z]+
匹配出字符串中的大写英文字符
1 2 3 4 5 6 7 8 |
test = "亲们,让我们在2018一起学习Python_Language" #匹配出test中大写英文字符 pattern = '[A-Z]+' re.findall(pattern, test) ['P', 'L'] |
[a-z]+
匹配出字符串中的小写英文字符
1 2 3 4 5 6 7 8 |
test = "亲们,让我们在2018一起学习Python_Language" #匹配出test中小写英文字符 pattern = '[a-z]+' re.findall(pattern, test) ['ython', 'anguage'] |
[A-Za-z0-9]+
匹配出字符串中的英文和数字信息
1 2 3 4 5 6 7 8 |
test = "亲们,让我们在2018一起学习Python_Language" #匹配出test中的英文和数字信息 pattern = '[A-Za-z0-9]+' re.findall(pattern, test) ['2018', 'Python', 'Language'] |
w+
识别以空格为间隔的字符串,得到其中的数字、字符、下划线。例如
1 2 3 4 5 6 7 |
test = "亲 们,让 我 们 在 2018 一 起 学 习 Python_Language" pattern = 'w+' re.findall(pattern, test) ['亲', '们', '让', '我', '们', '在', '2018', '一', '起', '学', '习', 'Python_Language'] |
[\u4e00-\u9fa5A-Za-z0-9_]+
匹配出字符串中的中英文、数字和下划线信息
1 2 3 4 5 6 7 8 |
test = "亲们,让我们!!在2018 一起学习Python_Language!" #匹配出test中的中英文、数字和下划线信息 pattern = '[\u4e00-\u9fa5A-Za-z0-9_]+' re.findall(pattern, test) ['亲们', '让我们', '在2018', '一起学习Python_Language'] |
特殊需求表达式

邮箱
[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+.[a-zA-Z0-9_-]+
匹配出字符串中的邮箱信息
1 2 3 4 5 6 7 8 |
test = "报名Python课程的联系手机18888886688, 电话0536-6666888, 邮箱 dadeng6688_hit@qq.com, 具体课程信息可登陆www.dadengpython.com" #匹配出test中的邮箱信息 pattern = '[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+.[a-zA-Z0-9_-]+' re.findall(pattern, test) ['dadeng6688_hit@qq.com'] |
url
http://[w+.]+
匹配出字符串中的域名信息
1 2 3 4 5 6 7 8 |
test = "报名Python课程的联系手机18888886688, 电话0536-6666888, 邮箱 dadeng6688_hit@qq.com, 具体课程信息可登陆http://www.dadengpython.com.cn" #匹配出test中的url信息 pattern = 'http://[w+.]+' re.findall(pattern, test) ['http://www.dadengpython.com.cn'] |
手机号
1[3|4|5|8][0-9]d{4,8}
匹配出字符串中的手机号码信息
1 2 3 4 5 6 7 8 |
test = "报名Python课程的联系手机18888886688, 电话0536-6666888, 邮箱 dadeng6688_hit@qq.com, 具体课程信息可登陆http://www.dadengpython.com.cn" #匹配出test中的手机信息 pattern = '1[3|4|5|8][0-9]d{4,8}' re.findall(pattern, test) ['18888886688'] |
电话号码
d{3}-d{8}|d{4}-d{7}
匹配出字符串中的电话号码信息
1 2 3 4 5 6 7 8 |
test = "报名Python课程的联系手机18888886688, 电话0536-6666888, 邮箱 dadeng6688_hit@qq.com, 具体课程信息可登陆http://www.dadengpython.com.cn" #匹配出test中的电话信息 pattern = 'd{3}-d{8}|d{4}-d{7}' re.findall(pattern, test) ['0536-6666888'] |
日期格式
形如2018-09-11,d{4}-d{1,2}-d{1,2}
匹配出字符串中的日期信息。
1 2 3 4 5 6 7 8 |
test = "报名Python课程截至日期2018-08-30。报名联系手机18888886688" #匹配出test中的日期信息 pattern = 'd{4}-d{1,2}-d{1,2}' re.findall(pattern, test) ['2018-08-30'] |
空行信息
[ s ]+
匹配出字符串的空行,可以用该空行做一些操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
test = """报名Python课程 截至日期2018-08-30。 报名联系手机 18888886688""" pattern = '[ s ]+' #使用空行去分割字符串 re.split(pattern, test) ['报名Python课程', '截至日期2018-08-30。', '报名联系手机', '18888886688'] test = """报名Python课程 截至日期2018-08-30。 报名联系手机 18888886688""" pattern = '[ s ]+' #将空行替换为空 re.sub(pattern, '', test) '报名Python课程截至日期2018-08-30。报名联系手机18888886688' |
其他 常用的正则匹配
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
<br />非负整数:^\d+$ 正整数:^[0-9]*[1-9][0-9]*$ 非正整数:^((-\d+)|(0+))$ 负整数:^-[0-9]*[1-9][0-9]*$ 整数:^-?\d+$ 非负浮点数:^\d+(\.\d+)?$ 正浮点数 : ^((0-9)+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)$ 非正浮点数:^((-\d+\.\d+)?)|(0+(\.0+)?))$ 负浮点数:^(-((正浮点数正则式)))$ 英文字符串:^[A-Za-z]+$ 英文大写串:^[A-Z]+$ 英文小写串:^[a-z]+$ 英文字符数字串:^[A-Za-z0-9]+$ 英数字加下划线串:^\w+$ E-mail地址:^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$ URL:^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\s*)?$ 或:^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$ 邮政编码:^[1-9]\d{5}$ 中文:^[\u0391-\uFFE5]+$ 电话号码:^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$ 手机号码:^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$ 双字节字符(包括汉字在内):^\x00-\xff 匹配首尾空格:(^\s*)|(\s*$)(像vbscript那样的trim函数) 匹配HTML标记:<(.*)>.*<\/\1>|<(.*) \/> 匹配空行:\n[\s| ]*\r 提取信息中的网络链接:(h|H)(r|R)(e|E)(f|F) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)? 提取信息中的邮件地址:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 提取信息中的图片链接:(s|S)(r|R)(c|C) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)? 提取信息中的IP地址:(\d+)\.(\d+)\.(\d+)\.(\d+) 提取信息中的中国手机号码:(86)*0*13\d{9} 提取信息中的中国固定电话号码:(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8} 提取信息中的中国电话号码(包括移动和固定电话):(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14} 提取信息中的中国邮政编码:[1-9]{1}(\d+){5} 提取信息中的浮点数(即小数):(-?\d*)\.?\d+ 提取信息中的任何数字 :(-?\d*)(\.\d+)? IP:(\d+)\.(\d+)\.(\d+)\.(\d+) 电话区号:/^0\d{2,3}$/ 腾讯QQ号:^[1-9]*[1-9][0-9]*$ 帐号(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$ 中文、英文、数字及下划线:^[\u4e00-\u9fa5_a-zA-Z0-9]+$ 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 匹配双字节字符(包括汉字在内):[^\x00-\xff] 匹配空行的正则表达式:\n[\s| ]*\r 匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/ sql语句:^(select|drop|delete|create|update|insert).*$ 匹配首尾空格的正则表达式:(^\s*)|(\s*$) 匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* |

