Python正则表达式re模块_第1页
Python正则表达式re模块_第2页
Python正则表达式re模块_第3页
Python正则表达式re模块_第4页
Python正则表达式re模块_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Python正则基本说明之前讲过关于Python正则的,都是理论的东西,现在讲讲Python正则re模块。导入re模块:import re 查看帮助文档:print re._doc_ 下面就是输出的帮助文档:Support for regular expressions (RE).This module provides regular expression matching operations similar tothose found in Perl. It supports both 8-bit and Unicode strings; boththe patter

2、n and the strings being processed can contain null bytes andcharacters outside the US ASCII range.Regular expressions can contain both special and ordinary characters.Most ordinary characters, like "A", "a", or "0", are the simplestregular expressions; they simply match

3、 themselves. You canconcatenate ordinary characters, so last matches the string 'last'.The special characters are: "." Matches any character except a newline. "" Matches the start of the string. "$" Matches the end of the string or just before the newline at the

4、 end of the string. "*" Matches 0 or more (greedy) repetitions of the preceding RE. Greedy means that it will match as many repetitions as possible. "+" Matches 1 or more (greedy) repetitions of the preceding RE. "?" Matches 0 or 1 (greedy) of the preceding RE. *?,+?,?

5、Non-greedy versions of the previous three special characters. m,n Matches from m to n repetitions of the preceding RE. m,n? Non-greedy version of the above. "" Either escapes special characters or signals a special sequence./FROM THIS WEBSITE : Indicates a set of characters. A ""

6、 as the first character indicates a complementing set. "|" A|B, creates an RE that will match either A or B. (.) Matches the RE inside the parentheses. The contents can be retrieved or matched later in the string. (?iLmsux) Set the I, L, M, S, U, or X flag for the RE (see below). (?:.) Non

7、-grouping version of regular parentheses. (?P<name>.) The substring matched by the group is accessible by name. (?P=name) Matches the text matched earlier by the group named name. (?#.) A comment; ignored. (?=.) Matches if . matches next, but doesn't consume the string. (?!.) Matches if .

8、doesn't match next. (?<=.) Matches if preceded by . (must be fixed length). (?<!.) Matches if not preceded by . (must be fixed length). (?(id/name)yes|no) Matches yes pattern if the group with id/name matched, the (optional) no pattern otherwise.The special sequences consist of ""

9、; and a character from the listbelow. If the ordinary character is not on the list, then theresulting RE will match the second character. number Matches the contents of the group of the same number. A Matches only at the start of the string. Z Matches only at the end of the string. b Matches the emp

10、ty string, but only at the start or end of a word. B Matches the empty string, but not at the start or end of a word. d Matches any decimal digit; equivalent to the set 0-9. D Matches any non-digit character; equivalent to the set 0-9. s Matches any whitespace character; equivalent to tnrfv. S Match

11、es any non-whitespace character; equiv. to tnrfv. w Matches any alphanumeric character; equivalent to a-zA-Z0-9_. With LOCALE, it will match the set 0-9_ plus characters defined as letters for the current locale. W Matches the complement of w. Matches a literal backslash.This module exports the foll

12、owing functions: match Match a regular expression pattern to the beginning of a string. search Search a string for the presence of a pattern. sub Substitute occurrences of a pattern found in a string. subn Same as sub, but also return the number of substitutions made. split Split a string by the occ

13、urrences of a pattern. findall Find all occurrences of a pattern in a string. finditer Return an iterator yielding a match object for each match. compile Compile a pattern into a RegexObject. purge Clear the regular expression cache. escape Backslash all non-alphanumerics in a string.Some of the fun

14、ctions in this module takes flags as optional parameters: I IGNORECASE Perform case-insensitive matching. L LOCALE Make w, W, b, B, dependent on the current locale. M MULTILINE "" matches the beginning of lines (after a newline) as well as the string. "$" matches the end of lines

15、 (before a newline) as well as the end of the string. S DOTALL "." matches any character at all, including the newline. X VERBOSE Ignore whitespace and comments for nicer looking RE's. U UNICODE Make w, W, b, B, dependent on the Unicode locale.This module also defines an exception '

16、;error'.上面说了基本语法和一些函数的使用。基本语法在上面链接已经说明。下面介绍主要函数的使用。re的函数说明match查看帮助:help(re.match)Help on function match in module re:match(pattern, string, flags=0) Try to apply the pattern at the start of the string, returning a match object, or None if no match was found.re.match(pattern, string, flags=0)功能:

17、从字符串string第一个位置开始匹配,根据建立的pattern规则匹配,返回匹配规则的的字符串。如果没有匹配成功返回:None.flags是可选参数,用于控制正则表达式的匹配方式。 例子:import repattern='w3.a-z+.(com)'str1=""str2="http:"re1=re.match(pattern,str1)print re1.group(0)re2=re.match(pattern,str2)print re2.group(0)匹配开始位置是 的网址,第一个输出 ,第二个竟然报错了,因为第一个

18、不匹配,但是说明文档说的是返回None的。search查看帮助:help(re.search)Help on function search in module re:search(pattern, string, flags=0) Scan through string looking for a match to the pattern, returning a match object, or None if no match was found.re.search(pattern, string, flags=0)功能:在字符串string中找到一个满足pattern匹配模式的字符串,

19、不存在的返回None例子:import repattern='w3.a-z+.(com)'str1=""str2="http:"re1=re.search(pattern,str1)print re1.group()re2=re.search(pattern,str2)print re2.group()第一个输出:,第二个:报错,匹配失败sub查看帮助: help(re.sub)Help on function sub in module re:sub(pattern, repl, string, count=0, flags=0) Re

20、turn the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it's passed the match object and must return a repl

21、acement string to be used.re.sub(pattern,repl,string,count=0,flags=0)功能:将字符串string满足pattern规则的字符串替换成repl,count默认是0全部替换,若是2是指只替换前两个。例子:import repattern='w3.a-z+.(com)'repl=''str3="i love ,tom love "re3=re.sub(pattern,repl,str3,1)print re3输出:i love ,tom love subn与re.sub 差不多只是

22、在返回时候还返回替换字符的个数 例子:import repattern='w3.a-z+.(com)'repl=''str3="i love ,tom love "re3=re.subn(pattern,repl,str3,2)print re3输出:(i love ,tom love , 2)split查看帮助: help(re.split)Help on function split in module re:split(pattern, string, maxsplit=0, flags=0) Split the source

23、 string by the occurrences of the pattern, returning a list containing the resulting substrings.re.split(pattern,string,maxsplit=0,flags=0)功能:根据pattern规则把字符串string分离,保存在list中。maxsplit是最大分类个数,默认最大。 例子:import restr="xiaoming,xiaohua,xiaoli,xiaoqiang,xiaozhang"pattern=","print

24、re.split(pattern,str)输出结果:xiaoming, xiaohua, xiaoli, xiaoqiang, xiaozhangfindall查看帮助:help(re.findall)Help on function findall in module re:findall(pattern, string, flags=0) Return a list of all non-overlapping matches in the string. If one or more groups are present in the pattern, return a list of

25、groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result.re.findall(pattern, string, flags=0)功能:在字符串string中找出所有满足正则的字符串,并存在列表list中,没有列表为空例子:import restr="xiaoming,xiaohua,xiaoli,xiaoqiang,xiaozhang"pattern="w+"print re

26、.findall(pattern,str)结果和上面的一样但是理解一样不一样的:xiaoming, xiaohua, xiaoli, xiaoqiang, xiaozhangfinditer和 findall 类似,在字符串中找到正则表达式所匹配的所有子串,并组成一个迭代器返回例子:import restr="xiaoming,xiaohua,xiaoli,xiaoqiang,xiaozhang"pattern="w+"re4= re.finditer(pattern,str)for i in re4: print i.group()迭代器,通过for循

27、环输出 for i in re4:. print i.group(). xiaomingxiaohuaxiaolixiaoqiangxiaozhangcompile查看帮助:help(pile)Help on function compile in module re:compile(pattern, flags=0) Compile a regular expression pattern, returning a pattern object.pile(pattern, flags=0)功能:把正则表达式pattern转化成正则表达式对象 例子:import restr="xiaoming,xiaohua,xiaoli,xiaoqiang,xiaozhang"pattern="w+"patternobj=pile(pattern)re4= re.finditer(pattern,str)for i in re4: print i.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论