1.Special Symbols and Characters

1.1 single regex 1

.  ,Match any character(except \n)

^  ,Match start of string

$  ,Match end of string

*  ,Match 0 or more occurrences preceding regex

+  ,Match 1 or more occurrences preceding regex

?  ,Match 0 or 1 occurrence preceding regex

{N}  ,Match N occurrences preceding regex

{M,N}  ,Match from M to N occurrences preceding regex

[...]  ,Match any single character from character class

[..x-y..]  ,Match any single character in the range from x to y ;["-a],In an ASCII system,all characters that fall between '"' and "a",that is ,between ordinals 34 and 97。

[^...]  ,Do not match any character from character class ,including any ranges ,if present

(*|+|?|{})?  ,Apply "non-greedy" versiongs of above occurrence/repetition symbols;默认情况下* + ? {}都是贪婪模式,在其后加上'?'就成了非贪婪模式。

(...)  ,Match enclosed regex and save as subgroup .

1.2 single regex 2

\d  ,Match any decimal digit ,same as [0-9](\D is inverse of \d:do not match any numeric digit)

\w  ,Match any alphanumeric character,same as [A-Za-z0-9](\W is inverse of \w)

\s  ,Match any whitespace character,same as [\n\t\r\v\f](\S is inverse of \s)

\b  ,Match any word boundary(\B is inverse of \b)

\N  ,Match saved subgroup N(see (...) above) ;exam:print(\1,\3,\16)

\c  ,transferred meaning ,without its special meaning;exam:\.,\\,\*

\A(\Z)  ,Match start (end) fo string (also see ^ and $ above)

1.3 complex regex

(?=...)  ,前向肯定断言。如果当前包含的正则表达式(这里以 ... 表示)在当前位置成功匹配,则代表成功,否则失败。一旦该部分正则表达式被匹配引擎尝试过,就不会继续进行匹配了;剩下的模式在此断言开始的地方继续尝试。举例:love(?=FishC) 只匹配后边紧跟着 FishC的字符串 love。

(?!...)  ,前向否定断言。这跟前向肯定断言相反(不匹配则表示成功,匹配表示失败)。举例:FishC(?!\.com)只匹配后边不是 .com& 的字符串 Fish。

(?<=...)  ,后向肯定断言。跟前向肯定断言一样,只是方向相反。举例:(?<=love)FishC 只匹配前边紧跟着 love 的字符串 FishC。

(?<!...)  ,后向否定断言。跟前向否定断言一样,只是方向相反。举例:(?<!FishC)\.com 只匹配前边不是 FishC的字符串 .com。

(?:)  ,该子组匹配的字符串无法从后面获取。

(?(id/name)yes-pattern|no-pattern)  ,1. 如果子组的序号或名字存在的话,则尝试 yes-pattern 匹配模式;否则尝试 no-pattern 匹配模式;

                 2. no-pattern 是可选的

                    举例:(<)?(\w+@\w+(?:\.\w+)+)(?(1)>|$) 是一个匹配邮件格式的正则表达式,可以匹配 <user@fishc.com>; 和 'user@fishc.com',但是不会匹配 '<user@fishc.com' 或 'user@fishc.com>'

1.4 匹配邮箱地址举例

import re

data = 'z843248880@163.com'
data1 = '<z843248880@163.com>'
data2 = '<z843248880@163.com'
data3 = 'z843248880@163.com>'
p1 = '(<)?(\w+@\w+(?:\.\w+)+)(?(1)>|$)'
p2 = '\w+@\w+\.\w+'
p3 = '(<)?\w+@\w+\.\w+(?(1)>|$)'
m1 = re.match(p3, data3)
print(m1.group())
PS:p1里的(?:\.\w+)代表这里的"\.\w+"匹配的字符串不会被后面获取;p1里的"(?(1)>|$)"表示,如果前面有“<",则此处匹配">",如果前面没有"<",则此处匹配结束符”$“,"(1)"代表的前面的第一个括号里的字符串,也就是"(<)";p1和p3的作用一样;p2不能排除仅有"<"或仅有">"的情况。

1.5 The re Modules:Core Functons and Methods

match(pattern,string,flags=0)  ,Attempt to match pattern to string with optional flags;return match object on success,None on failure;it is start of the string to match.

search(pattern,string,flags=0)  ,Search for first occurrence of pattern within string with optional flags;return match object on success,None on failure;it is start of the string to                 match.

findall(pattern,string[,flags=0])  ,Look for all occurrences of pattern in string;return a list of matches.

finditer(pattern,string[,flags=0])  ,Same as findall(),except returns an iterator instead of a list;for each match,the iterator returns a match object.

split(pattern,string,max=0)  ,Split string into a list according to regex pattern delimiter and return list of successful matches,aplitting at most max times(split all occurrences is the               default)

1.6 the usage of "?i" and "?m"

>>> import re
>>> re.findall(r'(?i)yes','yes Yes YES')
['yes', 'Yes', 'YES']
>>> re.findall(r'(?i)th\w+','The quickest way is through to this tunnel.')
['The', 'through', 'this']
>>> re.findall(r'(?im)(^th[\w ]+)',''')
... this line is the first,
... another line,
... that line,it's the best.
... ''')
['this line is the first', 'that line']
>>> re.findall(r'(?i)(^th[\w ]+)','''
... this line is the first,
... another line,
... that line ,it's the best.
... ''')
[]
>>> re.findall(r'(?i)(^th[\w \n,]+)','''
... this line is th,
... anonjkl line,
... that line,it the best.
... ''')
[]

By using "multiline" we can perform the search across multiple lines of the target string rather than treating the entire string as a single entity.

1.7 the usage of spilt

re.split(r'\s\s+',eachline)  ,at least two whitespace.

re.split(r'\s\s+|\t',eachline.rstrip())  ,at least two whitespace or one tablekey;rstrip(),delete the '\n'.

1.8 one example

from random import randrange,choice
from string import ascii_lowercase as lc
from sys import maxsize
from time import ctime

tlds = ('com','org','net','gov','edu')

for i in range(randrange(5,11)):
  dtint= randrange(1469880872)
  dstr = ctime(dtint)
  llen = randrange(4,8)
  login = ''.join(choice(lc) for j in range(llen))
  dlen = randrange(llen,13)
  dom = ''.join(choice(lc) for j in range(dlen))
  print('%s::%s@%s.%s::%d-%d-%d' % (dstr,login,dom,choice(tlds),dtint,llen,dlen))

result:

Sat Nov 7 01:09:06 1998::hbtua@yzhnjyjanwuq.gov::910372146-5-12
Sat Oct 17 09:27:56 2015::djbljsf@uidicjppd.gov::1445045276-7-9
Sun Nov 18 06:10:07 1979::fkobvlf@zlnlyjej.org::311724607-7-8
Wed Jul 23 17:23:03 1986::hovwgi@wiidgvnng.net::522490983-6-9
Tue Feb 24 02:15:27 1998::xnuab@sgahgahv.gov::888257727-5-8
Thu Jun 1 14:20:55 1989::rdwqhu@xzazufffut.net::612681655-6-10
Mon Mar 6 14:36:59 1978::qabkezi@sehnxqcuxexf.net::258014219-7-12
Sun Apr 11 15:01:56 1982::agzp@sygikhagdasq.gov::387356516-4-12

1.9 Matching a string

import re
data = 'Wed Jul 22 08:42:15 2015::qaolc@ombddhysxuv.com::1437525736-347-28'
#pat_old = '^Mon|^Tue|^Wed|^Thu|^Fri|^Sta|^Sun'
pat = '^(Mon|Tue|Wed|Thu|Fri|Sta|Sun)'
m = re.match(pat, data)
print(type(m))
print(m.group(0))

pat2 = '^(\w{3})'
m2 = re.match(pat2, data)
print(type(m2))
print(m2.group(1))

pa3 = '.+(\d+-\d+-\d+)'
m3 = re.search(pa3, data)
print(type(m3))
print(m3.group())
m4 = re.match(pa3, data)
print(m4.group(1))

pa4 = '.+?(\d+-\d+-\d+)'
m5 = re.match(pa4, data)
print(m5.group(1))

pa5 = '.+::(\d+-\d+-\d+)'
m6 = re.match(pa5, data)
print(m6.group(1))

result:

<class '_sre.SRE_Match' at 0x89df00>
Wed
<class '_sre.SRE_Match' at 0x89df00>
Wed
<class '_sre.SRE_Match' at 0x89df00>
Wed Jul 22 08:42:15 2015::qaolc@ombddhysxuv.com::1437525736-347-28
6-347-28                                  //greedy

1437525736-347-28                               //because the '?' behind of '.+',so none-greedy;(see above in 1.1)

1437525736-347-28

1.10 greedy and no-greedy

'.+' is greedy; '.+?' is not greedy.

2.用正则取出字母和数字,并将字母和数字分别输出

import re
str = 'python1班' print(re.search(r'(\w+)(\d)', str).group(0)) #取全部匹配的
print(re.search(r'(\w+)(\d)', str).group(1)) #取第一个括号匹配的
print(re.search(r'(\w+)(\d)', str).group(2)) #取第二个括号匹配的 结果:
python1
python
1

Jul_31 PYTHON REGULAR EXPRESSIONS的更多相关文章

  1. [Python] Regular Expressions

    1. regular expression Regular expression is a special sequence of characters that helps you match or ...

  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. 5.3.2 Eclipse集成开发环境的使用技巧

    Eclipse具有强大的编辑.调试.编译和打包功能,本节仅讲解Eclipse中最常用的功能. 1.将程序代码和注释字体变大 (1)启动Eclipse,选择“Windows”->“Preferen ...

  2. robotframework笔记15

    资源和变量文件 用户关键字和变量 测试用例文件 和 测试套件 初始化文件只能用于文件在哪里 了,但 资源文件 提供一种机制来分享它们. 自 资源文件结构非常接近测试用例文件,它是 容易创建它们. 变量 ...

  3. VBA控件一些属性的解释

    VBA每个控件都有很多属性,虽然可以按照分类排序,但由于没有中文解释,有些属性也不了解如何使用,下面是一些控件属性的解释,不全,可供参考: 常规AutoLoad (Excel)打开工作簿时是否加载控件 ...

  4. 对Web标准的理解

    Web标准是一系列标准的组合,包括结构化语言标准(XHTML/XML/HTML).表现标准语言(CSS)及行为标准语言(JavaScript).具体表现为: (1)Web标准规范要求,标签书写必须闭合 ...

  5. MVC之URL路由

    注册路由规则集合 一个 Web 应用具有一个全局的路由表,该路由表通过 System. Web.Routing.RouteTable的静态只读属性 Routes 表示,该属性返回一个类型为 Syste ...

  6. Java中Array的常用方法

    0.创建/声明一个数组 1 2 3 String[] aArray = new String[5]; String[] bArray = {"a","b",&q ...

  7. MASM6.15汇编程序例子

    /***************通过调用(INT 21H)表中的01h号功能号从键盘输入一个字符并回显到视频显示器上*****************/ DATAS SEGMENT ;此处输入数据段代 ...

  8. C#get,set

    一直对get,set的理解只在文字上:get 属性访问器用于返回属性值,而 set 访问器用于分配新值.其实这样理解是有点狭隘的,尤其是对set.set应该可以理解为为成员分配新值时的处理,比如一个类 ...

  9. ASP.NET-遇到的错误汇总

    错误:“未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序.” 在win7 64未上,读取Excel中的数据时报的错误, 解决方法:在生成"配置管理器中" ...

  10. 初学ExtJs 表格显示后台数据

    最近开始接触ExtJs,贴出自己的代码,一个简单的表格显示 版本 3.4.1 需要json包 代码清单1.jsp引入的ExtJs文件 <!-- 资源文件 ExtJs --> <lin ...