Python学习笔记6-Python中re(正则表达式)模块学习
2016-01-01 22:33
766 查看
今天学习了Python中有关正则表达式的知识。关于正则表达式的语法,不作过多解释,网上有许多学习的资料。这里主要介绍Python中常用的正则表达式处理函数。
import re
text ="JGood is a handsome boy, he is cool, clever, and so on..."
regex = re.compile(r'\w*oo\w*')
print regex.findall(text) #查找所有包含'oo'的单词print regex.sub(lambda m: '['+ m.group(0) +']', text) #将字符串中含有'oo'的单词用[]括起来。
转自:http://www.cnblogs.com/sevenyuan/archive/2010/12/06/1898075.html
re.match
re.match 尝试从字符串的开始匹配一个模式,如:下面的例子匹配第一个单词。import re
text ="JGood is a handsome boy, he is cool, clever, and so on..."
regex = re.compile(r'\w*oo\w*')
print regex.findall(text) #查找所有包含'oo'的单词print regex.sub(lambda m: '['+ m.group(0) +']', text) #将字符串中含有'oo'的单词用[]括起来。
转自:http://www.cnblogs.com/sevenyuan/archive/2010/12/06/1898075.html
相关文章推荐
- Python学习笔记5-字符串、bool、数值操作和数组字典排序
- Python学习笔记5-字符串、bool、数值操作和数组字典排序
- 安装python和添加tab补齐的一系列问题
- python初级学习之基于收发邮件的远程控制程序
- python中的切片
- Python 一些常用模块的安装
- Python安装easy_install报错的处理方式
- leetcode之Bulb Switcher
- 第1章 Python简介
- 第2章 Python基础语法--Hello python
- win64位 安装python opencv
- super函数
- leetcode之Wiggle Sort II
- 关于python的open函数的误区。
- Python Concurrent Programming
- python升级
- python升级
- python学习(三) 条件判断和循环
- Principle of Computing (Python)学习笔记(5) BFS Searching + Zombie Apocalypse
- Python学习笔记4-如何快速的学会一个Python的模块、方法、关键字