没有合适的资源?快使用搜索试试~ 我知道了~
python regular expressions.pdf 正则表达式
需积分: 9 2 下载量 84 浏览量
2020-02-14
20:26:37
上传
评论
收藏 91KB PDF 举报
温馨提示
试读
8页
Python版的正则表达式,全英文版,相比中文版的让人晦涩难懂,英文版让你更加深入了解其内在原理,英语使用简单,不涉及什么罕见词汇。相关的函数参数解释清楚,详细。
资源推荐
资源详情
资源评论
http://www.tutorialspoint.com/python/python_reg_expressions.htm
Copyright © tutorialspoint.com
PYTHON REGULAR EXPRESSIONS
PYTHON REGULAR EXPRESSIONS
A regular expression is a special sequence of characters that helps you match or find other strings
or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used
in UNIX world.
The module re provides full support for Perl-like regular expressions in Python. The re module
raises the exception re.error if an error occurs while compiling or using a regular expression.
We would cover two important functions, which would be used to handle regular expressions. But a
small thing first: There are various characters, which would have special meaning when they are
used in regular expression. To avoid any confusion while dealing with regular expressions, we
would use Raw Strings as r'expression'.
The match Function
This function attempts to match RE pattern to string with optional flags.
Here is the syntax for this function −
re.match(pattern, string, flags=0)
Here is the description of the parameters:
Parameter Description
pattern This is the regular expression to be matched.
string This is the string, which would be searched to match the pattern at
the beginning of string.
flags You can specify different flags using bitwise OR | . These are
modifiers, which are listed in the table below.
The re.match function returns a match object on success, None on failure. We usegroupnum or
groups function of match object to get matched expression.
Match Object Methods Description
groupnum = 0 This method returns entire match orspecificsubgroupnum
groups This method returns all matching subgroups in a tuple
emptyifthereweren
′
tany
Example
#!/usr/bin/python
im port re
line = "Cats are sm arter than dogs"
matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)
if matchObj:
print "matchObj.group() : ", matchObj.group()
print "matchObj.group(1) : ", matchObj.group(1)
print "matchObj.group(2) : ", matchObj.group(2)
资源评论
码Monkey
- 粉丝: 11
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- BLOCK_TYPE_HEARTBEAT_D70A3465D4EE4E9_046141_dump_1st.dmp
- 项目方法测试调用接口工具
- studyupdate
- 基于西瓜数据集的决策树实现.zip
- 60套HTML网站源码-响应式-涵盖(简历&作品展示&商业&科技&培训&商城&课设等)-适配移动设备-解压即用.zip
- 贪心算法要点和难点实例代码解析
- 65套HTML网站源码-响应式-涵盖(简历&作品展示&商业&科技&培训&商城&课设等)-适配移动设备-解压即用.zip
- 多因素决策树的Python实现.zip
- 使用Python在莺尾花数据集上实现了决策树算法,文件里有数据集.zip
- python实现决策树.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功