1.基本用法

#!/usr/bin/env python
# coding=utf-8 import re # example 1
text ="fjsk test\t fjskd bar\t \ttest"
regex = re.compile('\s+')
print regex.split(text) # example 2
email ="""
jfksdfasm@qq.com
test@test.com.cn
jfdskf@163.com
jkmiao@yahoo.123com
""" pattern = r'[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z.0-9]{2,6}'
regex = re.compile(pattern,flags=re.IGNORECASE) # get all
print regex.findall(email) # get the first one
m = regex.search(text)
# print email[m.start():m.end()]
print m # replace
print regex.sub('RECORD',email) 显示:
jkmiao@sysucis:~/workplace/python/test$ python regex.py
['fjsk', 'test', 'fjskd', 'bar', 'test']
['jfksdfasm@qq.com', 'test@test.com.cn', 'jfdskf@163.com', 'jkmiao@yahoo.123com']
None RECORD
RECORD
RECORD
RECORD

2. 分组,返回元组

#example 3

pattern = r'([A-Z0-9._%+=]+)@([A-Z0-9.-]+)\.([A-Z.]{2,5})'
regex = re.compile(pattern,flags=re.IGNORECASE)
m = regex.match('name@domain.suffix')
print m.groups() print regex.findall(email)
# output

('name', 'domain', 'suffi')
[('jfksdfasm', 'qq', 'com'), ('test', 'gamil', 'com'), ('jfdskf', '', 'com'), ('jkmiao', 'yahoo.com', 'cn')]

3.给分组加名称,返回字典

#example 4

regex = re.compile(r"""
(?P<userame>[A-Z0-9._%+-]+)
@(?P<domain>[A-Z0-9.-]+)
\.
(?P<suffix>[A-Z0-9.]{2,4})
""",flags=re.IGNORECASE|re.VERBOSE) m = regex.match("jkmaio@sysu.com")
print m.groupdict() print regex.findall(email) # output
jkmiao@sysucis:~/workplace/python/test$ python regex.py

{'domain': 'sysu', 'userame': 'jkmaio', 'suffix': 'com'}
[('jfksdfasm', 'qq', 'com'), ('test', 'gamil', 'com'), ('jfdskf', '', 'com'), ('jkmiao', 'yahoo.com', 'cn')]

python 正则表达式的处理的更多相关文章

  1. Python 正则表达式入门(中级篇)

    Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...

  2. Python正则表达式中的re.S

    title: Python正则表达式中的re.S date: 2014-12-21 09:55:54 categories: [Python] tags: [正则表达式,python] --- 在Py ...

  3. Python 正则表达式入门(初级篇)

    Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. 转载请写明出处 引子 首先说 正则表达式是什么? 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达 ...

  4. python正则表达式re

    Python正则表达式: re 正则表达式的元字符有. ^ $ * ? { [ ] | ( ).表示任意字符[]用来匹配一个指定的字符类别,所谓的字符类别就是你想匹配的一个字符集,对于字符集中的字符可 ...

  5. Python正则表达式详解

    我用双手成就你的梦想 python正则表达式 ^ 匹配开始 $ 匹配行尾 . 匹配出换行符以外的任何单个字符,使用-m选项允许其匹配换行符也是如此 [...] 匹配括号内任何当个字符(也有或的意思) ...

  6. 比较详细Python正则表达式操作指南(re使用)

    比较详细Python正则表达式操作指南(re使用) Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式.Python 1.5之前版本则是通过 regex 模块提供 E ...

  7. Python正则表达式学习摘要及资料

    摘要 在正则表达式中,如果直接给出字符,就是精确匹配. {m,n}? 对于前一个字符重复 m 到 n 次,并且取尽可能少的情况 在字符串'aaaaaa'中,a{2,4} 会匹配 4 个 a,但 a{2 ...

  8. python正则表达式 小例几则

    会用到的语法 正则字符 释义 举例 + 前面元素至少出现一次 ab+:ab.abbbb 等 * 前面元素出现0次或多次 ab*:a.ab.abb 等 ? 匹配前面的一次或0次 Ab?: A.Ab 等 ...

  9. Python 正则表达式-OK

    Python正则表达式入门 一. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分. 正则表达式是用于处理字符串的强大工具, 拥有自己独特的语法以及一个独立的处理引擎, 效率上 ...

  10. Python天天美味(15) - Python正则表达式操作指南(re使用)(转)

    http://www.cnblogs.com/coderzh/archive/2008/05/06/1185755.html 简介 Python 自1.5版本起增加了re 模块,它提供 Perl 风格 ...

随机推荐

  1. struts2.properties

    #action后缀struts.action.extension=action#上传文件的工作目录与文件的最大尺寸struts.multipart.saveDir=struts.multipart.m ...

  2. swig的语法和用法

    1.swig一般在js文件里引入方法: __inline(*.tpl),如下图所示 2.特殊用法 swig模板里套用swig模板,<link rel="import" hre ...

  3. UITableView去掉分隔符

    或用代码实现 [TableView setSeparatorColor:[UIColor clearColor]]; 问题一用你给的方法貌似不行,我用这个方法把分隔线给“去掉”了:    [editV ...

  4. WebGL编程指南案例解析之平移和旋转的math库实现

    这里说的math库实现,指的是,通过一般的加减乘除(角度计算)来更新坐标值. 因为涉及到坐标的变化,所以这里都是基于对顶点着色器的修改 平移: var vShader = ` attribute ve ...

  5. [Spring Boot] Spring Boot启动过程源码分析

    关于Spring Boot,已经有很多介绍其如何使用的文章了,本文从源代码(基于Spring-boot 1.5.6)的角度来看看Spring Boot的启动过程到底是怎么样的,为何以往纷繁复杂的配置到 ...

  6. Docker入门讲解

    1:容器重命名 [root@Docker ~]#docker run --name test_container -i -t centos /bin/bash[root@3ba67c6cf3f8 /] ...

  7. Qemu编译qemu-system-arm

    /********************************************************************************* * Qemu编译qemu-syst ...

  8. TJU Problem 2520 Quicksum

    注意: for (int i = 1; i <= aaa.length(); i++) 其中是“ i <= ",注意等号. 原题: 2520.   Quicksum Time L ...

  9. 本地和服务器(ubuntu)文件同步

    秘钥登录远端服务器 rsync -avze 'ssh -i ./id_rsa' root@remoteIp:/xx/remotefile.txt ./localpath (./id_rsa为本地秘钥路 ...

  10. HDU 1084:What Is Your Grade?

    Problem Description "Point, point, life of student!" This is a ballad(歌谣)well known in col ...