python re 正则表达式

夜深了,敲击键盘,用CSDN整理下python   re


正则表达式是含有文本和特别字符的字符串,这些文本和特别字符描述的模式可以识别各种字符串。

一下我们从实例结合理论来学习理解吧...

常用正则表达式符号:

记号                                                      说明                                                实例

re1|re2                                   匹配正则re1或者re2                                   foo|bar

.                                             匹配任何字符(换行符除外)                     b.b

^                                            匹配字符串的开始                                        ……Dear

$                                            匹配字符串的结尾                                       /bin/*sh$

*                                             匹配前面出现的正则0次或多次                  [A-Za-z0-9]*

+                                             匹配前面出现的正则1次或多次                 [a-z]+\.com

?                                             匹配前面出现的正则1次或多次                 goo?

{N}                                          匹配前面出现的正则N次                           [0-9]{3}

{M,N}                                      匹配重复出现M次到N次的正则                [0-9]{2,4}

[...]                                          匹配字符组里出现的任意一个字符          [aeiou]

[...x-y...]                                  匹配x到y中的任意一个字符                    [0-9],[A-Za-z]

[^...]                                        不匹配此字符集中的任意一个字符         [^aeiou]

(...)                                         匹配封闭括号中正则,并保存为子组     ([0-9]{3})?

特殊正则表达式符号:

\d                                            匹配任何数字,和[0-9]一样,(\D是\d的反义:任何非数符字)        data\d+.txt

\w                                           匹配任何数字字母字符,同[A-Za-z0-9]。(\W的反义)                     [A-Za-z]\w+

\s                                            匹配任何空白符,同[\n\t\r\v\f]。(\S的反义)                                     of\sthe

\b                                            匹配单词边界(\B的反义)                                                                 \bthe\b

\nn                                          匹配已保存的子组

\c                                            依次匹配特殊字符 c (即取消他的特殊含义,按照字面匹配)            \.,\\,\*

\A (\Z)                                     匹配字符串的起始(结束)                                                                  \ADear



实例:

import re
m = re.match('ab','abefghab')
m.groups()

m=re.match('foo','foo')
m.group()

m=re.match('ab|cd','abcdef')
m.group()

m=re.match('foo','food on the table')

m.group()

m=re.match('foo','fd on the foo')#error
m=re.search('foo','fd on the foo')

m=re.match('.end','bend the the')
m=re.match('.*end','how?bend the the')
m=re.match('.*end','how?\nbend the the')#error \n

m=re.match('[cr][23][dp][o2]','c2p2')
m=re.match('[cr][23][dp]?[o2]?','c2p2')
m=re.match('[cr][23][dp][o2]?','c2pippp')


m=re.match('\w+@(\w+\.)?\w+\.com','[email protected]')
m=re.match('\w+@(\w+\.)?\w+\.com','[email protected]')

m=re.match('\w\w\w-\d\d\d','abc-123')
m=re.match('(\w\w\w)-(\d\d\d)','abc-123') #m.groups()

m=re.match('(a)(b)','abcefg')#m.group(1)    m.group(2)    m.groups()


m=re.match('^the','the bank')
m=re.search('^the','end the')#error
m=re.search(r'\bthe','bite dog the')
m=re.search(r'\Bthe','bitethe dog')

re.findall('car','carry the barcardi to the car')
re.sub('X','Mr.Smith','attn:X\n\nDear X,\n')
re.subn('[ab]','X','agirlbeatshe')
re.split(':','str1:str2:str3')


con='jimi im you'
open('okay.txt','w').write(con)
a




你可能感兴趣的:(python re 正则表达式)