[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 ...
随机推荐
- FFmpeg再学习 -- SDL 环境搭建和视频显示
继续看雷霄骅的 课程资料 - 基于FFmpeg+SDL的视频播放器的制作 一.SDL 简介 参看:WIKI -- Simple DirectMedia Layer 参看:最简单的视音频播放示例9:SD ...
- word-break:break-all 打散文字,强制对齐
- 产生num个不重复的随机数组
createDiffRandom : function (from,to,num) { // 产生num个不重复的随机数组 var arr=[],json={}; // 随机数数组 , 标记json对 ...
- CentOS下glibc更新
使用java -agentpath=xxx时报该错,提示glibc版本过低:version `GLIBC_2.14' not found (required by /data/jjns/UEISecu ...
- Luogu 1452 Beauty Contest
Luogu 1452 Beauty Contest 求平面最远点对,先求出凸包,再找凸包的直径. 使用旋转卡壳,直径一定出现在对踵点对间.比较不同点到同一直线距离可以用叉积算三角形面积来比较. 实现时 ...
- BZOJ1131 POI2008 Sta 【树形DP】
BZOJ1131 POI2008 Sta Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=10 ...
- Codeforces 25E Test 【Hash】
Codeforces 25E Test E. Test Sometimes it is hard to prepare tests for programming problems. Now Bob ...
- WampServer的配置
转自:http://www.cnblogs.com/azumia/archive/2012/06/06/2538872.html 第一,打开局域网访问 配置文件:点击右下角的WAMP 服务器小托盘,选 ...
- 【Oracle】OGG单向复制配置
实验环境: 源端: Ip:192.168.40.10 DataBase:Oracle 11.2.0.1.0 ORCL OS:OEL5.6 OGG:fbo_ggs_Linux_x86_ora11g_32 ...
- js验证手机号码,邮箱,qq号
function validateMail(str:String) { var re_m = /^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2} ...