码迷,mamicode.com
首页 > 其他好文 > 详细

正则表达式

时间:2016-05-18 23:35:44      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

正则表达式: 

一,简介:

正则表达式(或RE)是一种小型的,高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现,再被编译成一系列的字节码,然后由用 C 编写的匹配引擎执行

二,字符匹配(普通字符,和元字符)

普通字符:大多数字符和字母都会和自身匹配
#         >>> re.findall(‘alex‘,‘yuanaleSxalexwupeiqi‘)
#            [‘alex‘]

元字符:

.匹配除换行符 \n之外的任何单字符。要匹配 .,请使用 \。

import re

print(re.findall(‘q.w‘,‘dsqdw‘)) #通配符匹配一个字符
print(re.findall(‘q..w‘,‘dsqdgw‘))# 匹配内容之间的内容 print(re.findall(‘a\.c‘,‘dsa.cdc‘)) #转义字符 使后一个字符改变原来的意思 D:\python3.5\python.exe D:/untitled/python3/day正则练习.py [‘qdw‘] [‘a.c‘] Process finished with exit code 0


^匹配输入字符串的开始位置\^

 

import re

print(re.findall(‘^a‘,‘asd‘)) 
print(re.findall(‘^as‘,‘asdf‘))
print(re.findall(‘^asd‘,‘asdfd‘))
print(re.findall(‘[^1-9]‘,‘ww3aa8.d‘)),‘ww3aa8.d‘))

D:\python3.5\python.exe D:/untitled/python3/lianxi.py
[‘a‘]
[‘as‘]
[‘asd‘]
[‘w‘, ‘w‘, ‘a‘, ‘a‘, ‘.‘, ‘d‘]

Process finished with exit code 0


$匹配输入字符串的结尾位置

 

print(re.findall(‘as$‘,‘dfas‘))
print(re.findall(‘fd$‘,‘asdfd‘))
print(re.findall(‘[1-9$]‘,‘ww3aa8.d‘))

D:\python3.5\python.exe D:/untitled/python3/lianxi.py
[‘as‘]
[‘fd‘]
[‘3‘, ‘8‘]

Process finished with exit code 0


*匹配当前子表达式0次或者多次 

 

import re
print(re.findall(‘abc*‘,‘abcdefg‘))
print(re.findall(‘as*‘,‘asssssddd‘))

D:\python3.5\python.exe D:/untitled/python3/lianxi.py
[‘abc‘]
[‘asssssss‘]

Process finished with exit code 0


+匹配前面的子表达式一次或多次。要匹配 + 字符,请使用 \+

 

print(re.findall(‘as+‘,‘asddddd‘))
print(re.findall(‘sd+‘,‘asdws

D:\python3.5\python.exe D:/untitled/python3/lianxi.py
[‘sd‘, ‘sdd‘]
[‘as‘]
dd‘))


?匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。要匹配 ? 字符,请使用 \?

 

print(re.findall(‘asc?‘,‘asa‘))
print(re.findall(‘azx?‘,‘azxazxs‘))

D:\python3.5\python.exe D:/untitled/python3/lianxi.py
[‘as‘]
[‘azx‘, ‘azx‘]

Process finished with exit code 0


{}匹配前一个字符m次

print(re.findall(‘ac{1}b‘,‘acbcc‘))

匹配前一个字符m次或n次
print(re.findall(‘ab{1,2}c‘,‘abbcbb‘))


D:\python3.5\python.exe D:/untitled/python3/lianxi.py
[‘acb‘]
[‘abbc‘]

Process finished with exit code 0


[]常用来制定一个字符集,如[ab]匹配 a 或b;其他的元字符在[]中不起作用,除了【-】【^】

print(re.findall(‘z[ds]‘,‘ewzds‘)) 
print(re.findall(‘z[cs]‘,‘ewz‘)) 


D:\python3.5\python.exe D:/untitled/python3/day正则练习.py
[‘zd‘]
[‘zc‘]

Process finished with exit code 0


|指明两项之间的一个选择。要匹配 |,请使用 \|

 

print(re.findall(‘zx|xz‘,‘zxssxzss‘))
print(re.findall(‘zxc|cxz‘,‘sdzxcscxzds‘))

D:\python3.5\python.exe D:/untitled/python3/lianxi.py
[‘zx‘, ‘xz‘]
[‘zxc‘, ‘cxz‘]

Process finished with exit code 0


()
被括起来的表达式将作为分组盲么,从表达式左边开始遇到每一个分组左括号(编号+1)

另外,分组表达式作为一个整体,可以后接数量词,表达式中 | 仅在改组有效

 

 

print(re.findall(‘(asd)‘,‘asdasdasd‘))
print(re.findall(‘za‘,‘aszasdza‘))
print(re.findall(‘(za){2}‘,"zaza"))
print(re.findall(‘a(12)|(23)c‘,‘a123c

D:\python3.5\python.exe D:/untitled/python3/lianxi.py
[‘asd‘, ‘asd‘, ‘asd‘]
[‘za‘, ‘za‘]
[‘za‘]
[(‘12‘, ‘‘)]

Process finished with exit code 0


\ 将下一个字符标记为或特殊字符、或原义字符、或向后引用、或八进制转义符

print(re.findall(‘a\.c‘,‘a.csd‘))
print(re.findall(r‘a\\c‘,‘a\cd‘))

D:\python3.5\python.exe D:/untitled/python3/lianxi.py
[‘a.c‘]
[‘a\\c‘]

Process finished with exit code 0

 
\d  匹配任何十进制数;它相当于类 [0-9]。
\D  匹配任何非数字字符;它相当于类 [^0-9]。
\s  匹配任何空白字符;它相当于类  [ \t\n\r\f\v]。
\S  匹配任何非空白字符;它相当于类 [^ \t\n\r\f\v]。
\w  匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]。
\W  匹配任何非字母数字字符;它相当于类 [^a-zA-Z0-9_]
\b: 匹配一个单词边界,也就是指单词和空格间的位置。  单词中间的符号代表两个,两边代表两个
\b 就是用在你匹配整个单词的时候。 如果不是整个单词就不匹配。

(r‘\d*‘,‘xin123456789‘)) #输出数字
print(re.findall(r‘\D*‘,‘kai1234!@#$%45623‘)) #非数字
print(re.findall(r‘\s*‘,‘kai    5132‘)) #匹配所有的空格
print(re.findall(r‘\S*‘,‘kai8465‘)) #匹配非空白字符串
print(re.findall(r‘\w*‘,‘kai#2454#%^%$#‘)) #匹配任何数字,字符串
print(re.findall(r‘\W*‘,‘kai^%$#5632‘)) #匹配任何非字符和数字
print(re.findall(r‘\b‘,‘asdadda‘)) #匹配一个单词边界,也就是指单词和空格键的位置
print(re.findall(r‘\bxin‘,‘xin,sad‘)) #匹配单词

 

正则表达式

标签:

原文地址:http://www.cnblogs.com/guokaixin/p/5507034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!