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的更多相关文章

  1. Jul_31 PYTHON REGULAR EXPRESSIONS

    1.Special Symbols and Characters 1.1 single regex 1 . ,Match any character(except \n) ^ ,Match start ...

  2. PCRE Perl Compatible Regular Expressions Learning

    catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...

  3. 正则表达式(Regular expressions)使用笔记

    Regular expressions are a powerful language for matching text patterns. This page gives a basic intr ...

  4. 【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 ...

  5. Python re module (regular expressions)

    regular expressions (RE) 简介 re模块是python中处理正在表达式的一个模块 r"""Support for regular expressi ...

  6. Python之Regular Expressions(正则表达式)

    在编写处理字符串的程序或网页时,经常会有查找符合某些复杂规则的字符串的需要.正则表达式就是用于描述这些规则的工具.换句话说,正则表达式就是记录文本规则的代码. 很可能你使用过Windows/Dos下用 ...

  7. Regular Expressions --正则表达式官方教程

    http://docs.oracle.com/javase/tutorial/essential/regex/index.html This lesson explains how to use th ...

  8. Introducing Regular Expressions 学习笔记

    Introducing Regular Expressions 读书笔记 工具: regexbuddy:http://download.csdn.net/tag/regexbuddy%E7%A0%B4 ...

  9. 8 Regular Expressions You Should Know

    Regular expressions are a language of their own. When you learn a new programming language, they're ...

随机推荐

  1. CUDA Samples: Calculate Histogram(atomicAdd)

    以下CUDA sample是分别用C++和CUDA实现的计算一维直方图,并对其中使用到的CUDA函数进行了解说,code参考了<GPU高性能编程CUDA实战>一书的第九章,各个文件内容如下 ...

  2. How to choose the number oftopics/partitions in a Kafka cluster?

    How to choose the number oftopics/partitions in a Kafka cluster? 如何为一个kafka集群选择topics/partitions的数量? ...

  3. Linux 大文件的分割与合并

    1.分割 -- split命令 可以指定按行数分割和按字节大小分割两种模式. (1) 按行数分割 $ large_file.txt new_file_prefix 加上-d,使用数字后缀:加上--ve ...

  4. new Date()相关获取当月天数和当月第一天

    var  myDate = new Date(); //获取本月第一天周几 var monthFirst = new Date(myDate.getFullYear(), parseInt(myDat ...

  5. 每天一个linux命令:【转载】rmdir命令

    今天学习一下linux中命令: rmdir命令.rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的.(注意,rm - r dir命令可代替rmdir,但是有很大危险性.)删 ...

  6. Spring 管理Filter和Servlet

    本文转载自:http://www.open-open.com/lib/view/open1417248512252.html 在使用spring容器的web应用中,业务对象间的依赖关系都可以用cont ...

  7. Uoj 129 寿司晚宴

    Uoj 129 寿司晚宴 显然合法性只与每个数所含的质因子有关,考虑状压 \(dp\) 若记录所有质因子状态显然爆炸,注意到每个数最多有一个超过 \(\sqrt 500\) 的大质因子,而其他的小质因 ...

  8. BZOJ3209 花神的数论题 【组合数学+数位DP+快速幂】*

    BZOJ3209 花神的数论题 Description 背景 众所周知,花神多年来凭借无边的神力狂虐各大 OJ.OI.CF.TC …… 当然也包括 CH 啦. 描述 话说花神这天又来讲课了.课后照例有 ...

  9. WordCount运行详解

    1.MapReduce理论简介 1.1 MapReduce编程模型 MapReduce采用"分而治之"的思想,把对大规模数据集的操作,分发给一个主节点管理下的各个分节点共同完成,然 ...

  10. 《DSP using MATLAB》示例 Example 6.25

    代码: % x = [-0.9, 0.81]; [y, L, B] = QCoeff(x, 3) % Unquantized parameters r = 0.9; theta = pi/3; a1 ...