[Python] Regular Expressions
1. regular expression
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.
2.re module
re module supports Perl-like regular expression.
The re module raises the exception re.error if an error occurs while compiling or using a regular expression.
To avoid any confusion while dealing with regular expressions, we would use Raw Strings as r'expression'.
3. match function
Syntax:
re.match(pattern, string, flags=0)
pattern #a regular expression to be matched
string #a string will be searched to match the pattern at the beginning of string
flags #modifiers. You can specify different flags using bitwise OR (|).
returns a match object on success, None on failure
Example:
import re line = "Cats are smarter 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)
else:
print "No match!!" #group() is Match Object Methods
#group() represent all the string
#group(1) represent one word before pattern in the string
#group(2) represent one word after pattern in the string
4. search function
#Syntax:
re.search(pattern, string, flags=0)
#pattern: This is the regular expression to be matched.
#string: This is the string, which would be searched to match the pattern anywhere in the string.
#flags: the same as match()
returns a match object on success, none on failure
Its group method is the same as match.
import re line = "Cats are smater than dogs." searchObj = re.search(r'(.*) are (.*?) .*', line, re.M|re.I) if searchObj:
print "searchObj.group(): ", searchObj.group()
print "searchObj.group(1): ", searchObj.group(1)
print "searchObj.group(2): ", searchObj.group(2)
else:
print "no match"
5. Match VS Search
match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string
import re line = "Cats are smater than dogs." searchObj = re.search(r'dogs', line, re.M|re.I)
matchObj = re.match(r'dogs', line, re.M|re.I) if searchObj:
print "searchObj.group(): ", searchObj.group()
else:
print "no match\n" if matchObj:
print "matchObj.group(): ", matchObj.group()
else:
print "no match\n
When the code is executed, it produced the following result:
searchObj.group(): Cats are smater than dogs.
no match
6. sub
#syntax:
re.sub(pattern, repl, string, max=0)
#This method replaces all occurrences of the RE pattern in string with repl,
#substituting all occurrences unless max provided.
#This method returns modified string.
Explame:
import re phone = "32580-110-517 #nhmhhh" #Delete python style comment
num = re.sub(r'#.*$', "", phone)
print "phone num:", num #Delete non-digit characters
num = re.sub(r'\D', "", phone)
print "phone num:", num
When the above code is executed, it produces the following result −
phone num:32580-110-517
phone num:32580110517
7. Regular Expression Modifiers: Option flags
You can provide multiple modifiers using exclusive OR (|).
re.I #Performs case-insensitive matching.
re.L #Interprets words according to the current locale.
re.M #Makes $ match the end of a line
#(not just the end of the string)
#makes ^ match the start of any line
#(not just the start of the string)
re.S #Makes a period (dot) match any character, including a newline.
re.U #Interprets letters according to the Unicode character set.
re.X #Permits "cuter" regular expression syntax. It ignores whitespace (except inside a set [] or when escaped by a backslash) and treats unescaped # as a comment marker.
8. Regular Expression Patterns
https://www.tutorialspoint.com/python/python_reg_expressions.htm
[Python] Regular Expressions的更多相关文章
- Jul_31 PYTHON REGULAR EXPRESSIONS
1.Special Symbols and Characters 1.1 single regex 1 . ,Match any character(except \n) ^ ,Match start ...
- PCRE Perl Compatible Regular Expressions Learning
catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...
- 正则表达式(Regular expressions)使用笔记
Regular expressions are a powerful language for matching text patterns. This page gives a basic intr ...
- 【Python学习笔记】Coursera课程《Using Python to Access Web Data 》 密歇根大学 Charles Severance——Week2 Regular Expressions课堂笔记
Coursera课程<Using Python to Access Web Data > 密歇根大学 Charles Severance Week2 Regular Expressions ...
- Python re module (regular expressions)
regular expressions (RE) 简介 re模块是python中处理正在表达式的一个模块 r"""Support for regular expressi ...
- Python之Regular Expressions(正则表达式)
在编写处理字符串的程序或网页时,经常会有查找符合某些复杂规则的字符串的需要.正则表达式就是用于描述这些规则的工具.换句话说,正则表达式就是记录文本规则的代码. 很可能你使用过Windows/Dos下用 ...
- Regular Expressions --正则表达式官方教程
http://docs.oracle.com/javase/tutorial/essential/regex/index.html This lesson explains how to use th ...
- Introducing Regular Expressions 学习笔记
Introducing Regular Expressions 读书笔记 工具: regexbuddy:http://download.csdn.net/tag/regexbuddy%E7%A0%B4 ...
- 8 Regular Expressions You Should Know
Regular expressions are a language of their own. When you learn a new programming language, they're ...
随机推荐
- React-Native基础_5.列表视图ListView 网络数据展示
//获取网络数据 并用列表展示 豆瓣Top250 api /** * Sample React Native App * https://github.com/facebook/react-nativ ...
- 从互联网进化的角度看AI+时代的巨头竞争
今天几乎所有的互联网公司在谈论和布局人工智能,收购相关企业.人工智能和AI+成为当今科技领域最灸手可热的名词,关于什么是AI+,其概念就是用以表达将"人工智能"作为当前行业科技化发 ...
- [Linux] ssh免密码登录
目标:本地机器ssh登录远程目标机器时不用输入密码 (默认状态下,ssh user@192.xxx.x.xxx需要输入密码) 原理:通过公钥和私钥实现系统认证 实现:把本地机器的公钥复制到目标机器 具 ...
- 数位DP新识
简单题:HDU2089 HDU3652 HDU4734 HDU3555 POJ3252 HigoCoder1033(需要前导0,或者用方法4) 总结: 1,dfs(pos,state, ...
- bzoj 2002 Bounce 弹飞绵羊
bzoj 2002 Bounce 弹飞绵羊 设一个虚拟节点表示被弹飞,则每个点的后继点是唯一确定的,每个点向它的后继点连边,就形成了一颗树. 询问就是问某个节点到虚拟节点的路径长度,修改就删除原来向后 ...
- LOJ2421 NOIP2015 信息传递 【tarjan求最小环】
LOJ2421 NOIP2015 信息传递 LINK 题目大意就是给你一个有向图,求最小环 有一个很奇妙的性质叫做每个点只有一条出边 然后我们考虑对每个强联通分量进行考虑 发现每个强联通分量内的边数一 ...
- Python学习-第三方库操作
2018-05-04 12:03:19 Python安装模块,更新模块 #显示模块 pip list #显示过期模块 pip list --outdated #安装模块 pip install x ...
- 微信小程序(3)——常用的组件
view: view是小程序中的视图容器之一,似于html中的<div>标签 <view class="section"> <view class=& ...
- @contextmanager
with的作用,类似try...finally...,提供一种上下文机制. 要应用with语句的类,其内部必须提供两个内置函数__enter__以及__exit__ , 前者在主体代码执行前执行, ...
- 使用Costura.Fody将源DLL合并到目标EXE
本文为原创文章,如转载,请在网页明显位置标明原文名称.作者及网址,谢谢! 一.本文主要是使用Costura.Fody工具将源DLL合并到目标EXE,因此,需要从以下任一链接下载: ①从Github地址 ...