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. EasyPlayer RTSP Android安卓播放器修复播放画面卡在第一帧bug

    本文转自EasyDarwin团队成员John的博客:http://blog.csdn.net/jyt0551/article/details/75717097 最近发现某些手机在运行EasyPlaye ...

  2. Linux运维-Rsync+Inotify

      Rsync+Inotify Rsync:linux系统下的数据镜像备份工具.使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH.rsync主机同步. 特性: 可 ...

  3. .NET Web开发技术简单整理 转

    .NET Web开发技术简单整理 原文:http://www.cnblogs.com/SanMaoSpace/p/3157293.html 在最初学习一些编程语言.一些编程技术的时候,做的更多的是如何 ...

  4. Swift GCD

    var queue: dispatch_queue_t = dispatch_get_main_queue()// 主线程 queue = dispatch_get_global_queue(DISP ...

  5. Ubuntu下快速建立跨多个平台的cocos2d-x项目

    原文:http://www.bennyxu.com/archives/462 这里之讲一点就是如何快速的建立起cocos2d-x项目,同时linux平台的优越性也充分的暴露无遗. 这里默认您已经成功的 ...

  6. [译]贵宾犬咬伤了TLS

    原文链接:https://community.qualys.com/blogs/securitylabs/2014/12/08/poodle-bites-tls 原文发表时间:2014.12.8 今天 ...

  7. JSP、Java和Servlet获取当前工程的路径

    JSP.Java和Servlet获取当前工程的路径: 1.JSP中取得路径: 以工程名为TEST为例: (1)得到包含工程名的当前页面全路径:request.getRequestURI()结果:/TE ...

  8. .NET/C# 使用 Span 为字符串处理提升性能

    .NET Core 2.1 和 C# 7.2 带来了 Span 的原生支持,原本需要使用不安全代码操作的内存块现在可以使用安全的方式来完成.此前在性能和稳定性上需要有所取舍,而现在可以兼得了. 简单的 ...

  9. 用eclipse运行java程序显示找不到main class,网上的方法都试了,还是不行,有没有知道怎么解决的呀!

    编译器问题,jdk版本不对,Window --> Preferences -->Java --> compiler中的compiler compliance level,这里选你当前 ...

  10. selenium 上传文件方法补充——SendKeys、win32gui

    之前和大家说了input标签的上传文件的方式: <selenium2 python 自动化测试实战>(13)——上传文件 现在好多网站上传的标签并不是input,而是div之类的比如: 全 ...