python-re使用举例
代码:
import re text = "JGood is a handsome boy, he is cool, clever, and so on..."
print(text)
m = re.match(r"(\w+)\s", text)
n = re.search(r"\shan(ds)ome\s", text)
if m:
print("match: \"(\w+)\s\" ")
print(m.group(0))
else:
print 'not match' if n:
print("search: \"\shan(ds)ome\s\"")
print(n.group(0))
else:
print 'not match'
执行结果:
JGood is a handsome boy, he is cool, clever, and so on...
match: "(\w+)\s"
JGood
search: "\shan(ds)ome\s"
handsome
参考:
http://www.cnblogs.com/sevenyuan/archive/2010/12/06/1898075.html
正则表达式参考:
http://www.runoob.com/regexp/regexp-syntax.html
https://msdn.microsoft.com/zh-cn/library/ae5bf541(v=vs.80).aspx
python-re使用举例的更多相关文章
- python yield用法举例说明
1 yield基本用法 典型的例子: 斐波那契(Fibonacci)數列是一个非常简单的递归数列,除第一个和第二个数外,任意一个数都可由前两个数相加得到.1 2 3 5 8…… def fab(ma ...
- 平时代码中不符合python风格的举例
良好的代码风格体现出自己的专业,良好的代码风格,方便同事之间协作. 下面举例讲讲自己在代码中不符合python风格的一些情况,方便自己写出优雅的代码. 段落之间空两行 很长的参数用括号连接但是不要超过 ...
- Python装饰器举例分析
概述 装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象. 我们要需要一个能测试函数运行时间的decorator,可以定义如 ...
- python之多线程举例
# 多线程举例 from threading import Thread from threading import current_thread class messager(Thread): de ...
- python with用法举例
我们知道在操作文件对象的时候可以这么写 with open('a.txt') as f: '代码块' 上述叫做上下文管理协议,即with语句,为了让一个对象兼容with语句,必须在这个对象的类中声明_ ...
- python字符串调用举例
以如下打印为例: my name is tom and my age is 12 方式一:字符串格式化表达式 name = 'tom' age = 12 print("my name is ...
- python之工作举例:通过复制NC文件来造数据
# 通过对NC文件复制来造数据 import os, shutil # 遍历的根目录 root_dir = "D:\\test_data\\DISASTER\\" # 获取NC文件 ...
- Python进程池举例
multiprocessing包 from multiprocessing import Pool def a(num): print num if __name__ == "__main_ ...
- 常用正则表达式最强汇总(含Python代码举例讲解+爬虫实战)
大家好,我是辰哥~ 本文带大家学习正则表达式,并通过python代码举例讲解常用的正则表达式 最后实战爬取小说网页:重点在于爬取的网页通过正则表达式进行解析. 正则表达式语法 Python的re模块( ...
- raw_input() 与 input() __ Python
这两个均是 python 的内建函数,通过读取控制台的输入与用户实现交互.但他们的功能不尽相同.举两个小例子. 1 >>> raw_input_A = raw_input(" ...
随机推荐
- css3背景及字体渐变
1.背景渐变: .linear { width: 100%; FILTER: progid:DXImageTransform.Microsoft.Gradient(gradientType=0,sta ...
- nginx+thinkphp pathinfo模式配置
server { listen 81; server_name http://lanxing.cc gohosts.com; root "D:\WWW\lanxing\public" ...
- UML类图组成
本文转载至 http://blog.csdn.net/fengsh998/article/details/8105666 UML类图的相关知识,UML类图(Classdiagram)是最常用的 ...
- iOS Dev (26) 初步了解下UIColor的最常用知识
作者:CSDN 大锐哥 地址:http://blog.csdn.net/prevention - 内置的颜色有啥? // Some convenience methods to create colo ...
- 在给mysql数据库备份时,报错: mysqldump: Got error: 145: Table '.\shengdaxcom\pre_forum_thread' is marked as c rashed and should be repaired when using LOCK TABLES
在给mysql数据库备份时,报错: mysqldump: Got error: 145: Table '.\shengdaxcom\pre_forum_thread' is marked as cra ...
- No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386).错误解决方法
targets ->build setting 下的 Build Active Architecture Only 设置 NO 就可以.
- (深入理解计算机系统)AT&T汇编指令
AT&T汇编指令学习(GCC) 寄存器命名原则: 相比inter语法,AT&T语法格式要求所有的寄存器都必须加上取值符"%". 操作码命令格式: 源/目的操作数顺序 ...
- platform_set_drvdata()/platform_get_drvdata()/container_of()【转】
本文转载自:http://blog.csdn.net/angle_birds/article/details/8443695 platform_set_drvdata(struct platform_ ...
- Zookeeper集群搭建安装
三台 Linux虚拟机,每台都需要安装Jdk环境 1.上传Zookeeper安装包 (比较大 直接上传得了) 或者:wget https://mirrors.tuna.tsinghua.edu.cn/ ...
- Oracle数据查看被锁住的用户
//lock_date是被锁住时间,如果为空证明这个用户没有被锁住 select username,lock_date from dba_users where username='GFMIS'; ...