python-正则表达式练习题
1、匹配一行文字中的所有开头的字母内容
#coding=utf-8
import re
s="i love you not because of who you are, but because of who i am when i am with you"
content=re.findall(r"\b\w",s)
print content
c:\Python27\Scripts>python task_test.py
['i', 'l', 'y', 'n', 'b', 'o', 'w', 'y', 'a', 'b', 'b', 'o', 'w', 'i', 'a', 'w', 'i', 'a', 'w', 'y']
2、匹配一行文字中的所有开头的数字内容
import re
s="i love you not because 12sd 34er 56df e4 54434"
content=re.findall(r"\b\d",s)
print content
c:\Python27\Scripts>python task_test.py
['1', '3', '5', '5']
3、匹配一行文字中的所有开头的数字内容或数字内容
>>> print re.match(r"\w+","123sdf").group()
123sdf
4、 只匹配包含字母和数字的行
#coding=utf-8
import re
s="i love you not because\n12sd 34er 56\ndf e4 54434"
content=re.findall(r"\w+",s,re.M)
print content
c:\Python27\Scripts>python task_test.py
['i', 'love', 'you', 'not', 'because', '12sd', '34er', '56', 'df', 'e4', '54434']
5、写一个正则表达式,使其能同时识别下面所有的字符串:'bat', 'bit', 'but', 'hat', 'hit', 'hut‘
import re
s="'bat', 'bit', 'but', 'hat', 'hit', 'hut"
content=re.findall(r"..t",s)
print content
c:\Python27\Scripts>python task_test.py
['bat', 'bit', 'but', 'hat', 'hit', 'hut']
6、匹配所有合法的python标识符
#coding=utf-8
import re
s="awoeur awier !@# @#4_-asdf3$^&()+?><dfg$\n$"
content=re.findall(r".*",s,re.DOTALL)
print s
print content
c:\Python27\Scripts>python task_test.py
awoeur awier !@# @#4_-asdf3$^&()+?><dfg$
$
['awoeur awier !@# @#4_-asdf3$^&()+?><dfg$\n$', '']
7、提取每行中完整的年月日和时间字段
#coding=utf-8
import re
s="""se234 1987-02-09 07:30:00
1987-02-10 07:25:00"""
content=re.findall(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}",s,re.M)
print s
print content
c:\Python27\Scripts>python task_test.py
se234 1987-02-09 07:30:00
1987-02-10 07:25:00
['1987-02-09 07:30:00', '1987-02-10 07:25:00']
8、将每行中的电子邮件地址替换为你自己的电子邮件地址
#coding=utf-8
import re
s="""693152032@qq.com, werksdf@163.com, sdf@sina.com
sfjsdf@139.com, soifsdfj@134.com
pwoeir423@123.com"""
content=re.sub(r"\w+@\w+.com","xiaxiaoxu1987@163.com",s)
print s
print "_______________________________________"
print content
c:\Python27\Scripts>python task_test.py
693152032@qq.com, werksdf@163.com, sdf@sina.com
sfjsdf@139.com, soifsdfj@134.com
pwoeir423@123.com
_______________________________________
xiaxiaoxu1987@163.com, xiaxiaoxu1987@163.com, xiaxiaoxu1987@163.com
xiaxiaoxu1987@163.com, xiaxiaoxu1987@163.com
xiaxiaoxu1987@163.com
9、匹配\home关键字:
>>> re.findall(r"\\home","skjdfoijower \home \homewer")
['\\home', '\\home']
1、使用正则提取出字符串中的单词
#coding=utf-8
import re
s="""i love you not because of who 234 you are, 234 but 3234ser because of who i am when i am with you"""
content=re.findall(r"\b[a-zA-Z]+\b",s)
print content
c:\Python27\Scripts>python task_test.py
['i', 'love', 'you', 'not', 'because', 'of', 'who', 'you', 'are', 'but', 'because', 'of', 'who', 'i', 'am', 'when', 'i', 'am', 'with', 'you']
2、使用正则表达式匹配合法的邮件地址:
import re
s="""xiasd@163.com, sdlfkj@.com sdflkj@180.com solodfdsf@123.com sdlfjxiaori@139.com saldkfj.com oisdfo@.sodf.com.com"""
content=re.findall(r"\w+@\w+.com",s)
print content
c:\Python27\Scripts>python task_test.py
['xiasd@163.com', 'sdflkj@180.com', 'solodfdsf@123.com', 'sdlfjxiaori@139.com']
python-正则表达式练习题的更多相关文章
- python 正则表达式 练习题
会用到的语法 正则字符 释义 举例 + 前面元素至少出现一次 ab+:ab.abbbb 等 * 前面元素出现0次或多次 ab*:a.ab.abb 等 ? 匹配前面的一次或0次 Ab?: A.Ab 等 ...
- python正则表达式练习题
# coding=utf-8 import re # 1. 写一个正则表达式,使其能同时识别下面所有的字符串:'bat','bit', 'but', 'hat', 'hit', 'hut' s =&q ...
- python正则表达式与re模块-02
正则表达式 正则表达式与python的关系 # 正则表达式不是Python独有的,它是一门独立的技术,所有的编程语言都可以使用正则 # 但要在python中使用正则表达式,就必须依赖于python内置 ...
- Python 正则表达式入门(中级篇)
Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...
- Python正则表达式中的re.S
title: Python正则表达式中的re.S date: 2014-12-21 09:55:54 categories: [Python] tags: [正则表达式,python] --- 在Py ...
- Python 正则表达式入门(初级篇)
Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. 转载请写明出处 引子 首先说 正则表达式是什么? 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达 ...
- python正则表达式re
Python正则表达式: re 正则表达式的元字符有. ^ $ * ? { [ ] | ( ).表示任意字符[]用来匹配一个指定的字符类别,所谓的字符类别就是你想匹配的一个字符集,对于字符集中的字符可 ...
- Python正则表达式详解
我用双手成就你的梦想 python正则表达式 ^ 匹配开始 $ 匹配行尾 . 匹配出换行符以外的任何单个字符,使用-m选项允许其匹配换行符也是如此 [...] 匹配括号内任何当个字符(也有或的意思) ...
- 比较详细Python正则表达式操作指南(re使用)
比较详细Python正则表达式操作指南(re使用) Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式.Python 1.5之前版本则是通过 regex 模块提供 E ...
- python入门练习题1
常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...
随机推荐
- selenium中javascript调试
之前写了使用js输入长文件的文章,有同事在使用时,发现竟然无法输入,也不知道是什么原因,且用的还是id方式. 在参考网文后,才发现是js写的有问题,现总结一下 javascript调试,在firefo ...
- Shell if else
语句1if [ expression ]then ...fi 语句2if [ expression ]then ...else ...fi 语句3if [ expression 1 ]then ... ...
- C++和Java中的静态Static用法
C++和Java中都有的一个静态关键字Static,可以放在类中的变量或者函数之前,就成了静态变量或者静态函数. 静态变量又分为静态全局变量和静态局部变量,可参见网上大神总结的C++全局变量,局部变量 ...
- mysql count(*) 和count(列) 的区别
count(*) 是统计包含null的记录,而count(列)不含null; 在不带where的情况下count(*)与count(列)相比,并非统计所有列,而是忽略所有列而直接统计行数; 当coun ...
- 微信小程序中target与currentTarget
target在事件流的目标阶段:currentTarget在事件流的捕获,目标及冒泡阶段.但事件流处于目标阶段,target与currentTarget指向一样, 而当处于捕获和冒泡阶段的时候,tar ...
- Bazel构建工具的安装
官方Doc:https://docs.bazel.build/versions/master/install-ubuntu.html 使用Bazel定制的APT存储库 (recommended) 1. ...
- POJ 1066 - Treasure Hunt - [枚举+判断线段相交]
题目链接:http://poj.org/problem?id=1066 Time Limit: 1000MS Memory Limit: 10000K Description Archeologist ...
- YARN Architecture
The fundamental idea of YARN is to split up the functionalities of resource management and job sched ...
- Oracle体系结构之Oracle静态监听配置模板
1.监听程序配置:[oracle@localhost admin]$ vim listener.ora# listener.ora Network Configuration File: /u01/a ...
- date 命令之日期和秒数转换
时间转为秒数 date -d "2012-11-12 13:00:00" +"%s" 描述转为日期 date -d@1352692800 +"%Y-% ...