1. 程式人生 > >Python數據分析學習-re正則表達式模塊

Python數據分析學習-re正則表達式模塊

如果 dal nds 隨機數 choice 而且 inf groups 對象

正則表達式 為高級的文本模式匹配、抽取、與/或文本形式的搜索和替換功能提供了基礎。簡單地說,正則表達式(簡稱為 regex)是一些由字符和特殊符號組成的字符串,它們描述了模式的重復或者表述多個字符,於是正則表達式能按照某種模式匹配一系列有相似特征的字符串。換句話說,它們能夠匹配多個字符串……一種只能匹配一個字符串的正則表達式模式是很乏味並且毫無作用的,不是嗎?Python 通過標準庫中的 re 模塊來支持正則表達式

正則表達式的特殊字符列表

‘.‘ 匹配所有字符串,除\n以外
‘-’ 表示範圍[0-9]
‘*‘ 匹配前面的子表達式零次或多次。要匹配 * 字符,請使用 \*。
‘+‘ 匹配前面的子表達式一次或多次。要匹配 + 字符,請使用 \+
‘^‘

匹配字符串開頭
‘$’ 匹配字符串結尾 re
‘‘ 轉義字符, 使後一個字符改變原來的意思,如果字符串中有字符*需要匹配,可以\*
‘?’ 匹配前一個字符串0次或1次
‘{m}‘ 匹配前一個字符m次
‘{n,m}‘ 匹配前一個字符n到m次
‘\d‘ 匹配數字,等於[0-9]
‘\D‘ 匹配非數字,等於[^0-9]
‘\w‘ 匹配字母和數字,等於[A-Za-z0-9]
‘\W‘ 匹配非英文字母和數字,等於[^A-Za-z0-9]
‘\s‘ 匹配空白字符
‘\S‘ 匹配非空白字符
‘\A‘ 匹配字符串開頭
‘\Z‘ 匹配字符串結尾
‘\b‘ 匹配單詞的詞首和詞尾,單詞被定義為一個字母數字序列,因此詞尾是用空白符或非字母數字符來表示的
‘\B‘
與\b相反,只在當前位置不在單詞邊界時匹配
‘(?P 分組,除了原有編號外在指定一個額外的別名
[] 是定義匹配的字符範圍。比如 [a-zA-Z0-9] 表示相應位置的字符要匹配英文字符和數字。[\s*]表示空格或者*號。

Python的re正則表達式模塊提供的方法

re.match(pattern, string, flags=0)      #從字符串的起始位置匹配,如果起始位置匹配不成功的話,match()就返回none
re.search(pattern, string, flags=0)     #掃描整個字符串並返回第一個成功的匹配
re.findall(pattern, string, flags=
0) #找到RE匹配的所有字符串,並把他們作為一個列表返回 re.finditer(pattern, string, flags=0) #找到RE匹配的所有字符串,並把他們作為一個叠代器返回 re.sub(pattern, repl, string, count=0, flags=0) #替換匹配到的字符串

函數參數說明:

pattern: 匹配的正則表達式 string:要匹配的字符串
flags: 標記為,用於控制正則表達式的匹配方式,如:是否區分大小寫,多行匹配等等。
repl: 替換的字符串,也可作為一個函數
count: 模式匹配後替換的最大次數,默認0表示替換所有匹配

import re

m = re.match(r‘f..‘, r‘begin fool hello‘) # match 從字符串的開始位置進行搜索,如果需從字符串任意位置進行搜索,需使用下文中的search方法
if m is not None:
    print(‘found : ‘ + m.group())
else:
    print(‘not found!‘)
not found!

search()函數不但會搜索模式在字符串中第一次出現的位置,而且嚴格地對字符串從左到右搜索。

m = re.search(r‘foo‘, ‘beginfool hello‘)
if m is not None:
    print(‘found : ‘ + m.group())
else:
    print(‘not found...‘)
found : foo

匹配任何單個字符串

anyend = ‘.end‘
m = re.match(anyend, ‘bend‘) # 點號匹配‘b’
if m is not None: print(m.group())
bend
m = re.match(anyend, ‘end‘) # 不匹配任何字符串
if m is not None: print(m.group())
m = re.match(anyend, \nbend‘) # 除了‘\n‘之外的任何字符串
if m is not None: print(m.group())
m = re.match(anyend, ‘The end.‘) # 點號匹配‘ end’
if m is not None: print(m.group())

gorup()和groups()方法的使用

m = re.match(r‘(\w{3})-(\d{3})‘, ‘abc-123‘)
if m is not None:
    print(‘m.group(): ‘ + m.group())
    print(‘m.group(1): ‘ + m.group(1))
    print(‘m.group(2): ‘ + m.group(2))
    print(‘m.groups(): ‘ + str(m.groups()))
m.group(): abc-123
m.group(1): abc
m.group(2): 123
m.groups(): (‘abc‘, ‘123‘)

findall()查詢字符串中某個正則表達式模式全部的非重復出現情況。這與 search()在執行字符串搜索時類似,但與 match()和 search()的不同之處在於,findall()總是返回一個列表。如果 findall()沒有找到匹配的部分,就返回一個空列表,但如果匹配成功,列表將包含所有成功的匹配部分(從左向右按出現順序排列)。

re.findall(‘car‘, ‘car‘)
[‘car‘]
re.findall(‘car‘, ‘scary‘)
[‘car‘]
re.findall(‘car‘, ‘carry the brcardi to the car‘)
[‘car‘, ‘car‘, ‘car‘]

finditer()和findall()返回的匹配字符串相比,finditer()在匹配對象中叠代.

s = ‘This and that.‘
re.findall(r‘(th\w+)‘, s, re.I)
[‘This‘, ‘that‘]
iter = re.finditer(r‘(th\w+)‘, s, re.I)
iter
<callable_iterator at 0x594a780>
[g.group() for g in iter] # findall 返回一個列表,而finditer返回一個叠代器
[]

有兩個函數/方法用於實現搜索和替換功能:sub()和 subn()。兩者幾乎一樣,都是將某字符串中所有匹配正則表達式的部分進行某種形式的替換。用來替換的部分通常是一個字符串,但它也可能是一個函數,該函數返回一個用來替換的字符串。subn()和 sub()一樣,但 subn()還返回一個表示替換的總數,替換後的字符串和表示替換總數的數字一起作為一個擁有兩個元素的元組返回。

print(re.sub(‘X‘, ‘Mr. Iceman‘, ‘attn: X\n\nDear X,\n))
attn: Mr. Iceman 
Dear Mr. Iceman,
print(re.subn(‘X‘, ‘Mr. Iceman‘, ‘attn: X\n\nDear X,\n))
(‘attn: Mr. Iceman\n\nDear Mr. Iceman,\n‘, 2)

re 模塊和正則表達式的對象方法 split()對於相對應字符串的工作方式是類似的,但是與分割一個固定字符串相比,它們基於正則表達式的模式分隔字符串,為字符串分隔功能添加一些額外的威力。

如果給定分隔符不是使用特殊符號來匹配多重模式的正則表達式,那麽 re.split()與str.split()的工作方式相同

re.split(‘:‘, ‘str1:str2:str3‘)
[‘str1‘, ‘str2‘, ‘str3‘]
DATA = (
    ‘Mountain View, CA 94040‘,
    ‘Sunnyvale, CA‘,
    ‘Los Altos, 94023‘,
    ‘Cupertino 95014‘,
    ‘Palo Alto CA‘
)

for item  in DATA:
    print( re.split(‘, |(?= (?:\d{5}|[A-Z]{2})) ‘, item))
[‘Mountain View‘, ‘CA‘, ‘94040‘]
[‘Sunnyvale‘, ‘CA‘]
[‘Los Altos‘, ‘94023‘]
[‘Cupertino‘, ‘95014‘]
[‘Palo Alto‘, ‘CA‘]
import os
import re

with os.popen(‘tasklist /nh‘, ‘r‘) as f:
    for line in list(f)[:5]:
        # print(re.split(r‘\s\s+|\t‘, line.rstrip())) #pid 和會話名未分解
        print(re.findall(r‘([\w.]+(?: [\w.]+)*)\s\s*(\d+)\s(\w+)\s\s*(\d+)\s\s*([\d,]+\sK)‘, line.strip()))
[]
[(‘System Idle Process‘, ‘0‘, ‘Services‘, ‘0‘, ‘24 K‘)]
[(‘System‘, ‘4‘, ‘Services‘, ‘0‘, ‘2,852 K‘)]
[(‘smss.exe‘, ‘364‘, ‘Services‘, ‘0‘, ‘1,268 K‘)]
[(‘csrss.exe‘, ‘612‘, ‘Services‘, ‘0‘, ‘6,648 K‘)]

如下以一完整示例結束本文,它以不同的方式使用正則表達式來操作字符串。首先使用該腳本為正則表達式練習創建隨機數據,然後將生成的數據提取其中的數字和郵箱地址

from random import randrange, choice
from string import ascii_lowercase as lc
from datetime import datetime
import time
import re

result_data = []

# gen data
tlds = (‘com‘, ‘cn‘, ‘edu‘, ‘net‘, ‘gov‘, ‘org‘)
for i in range(randrange(4, 9)):
    max_seconds = int(datetime.now().timestamp())
    dtint = randrange(max_seconds)
    #dtstr = str(datetime.fromtimestamp(dtint))
    dtstr = 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))
    result_data.append(%s::%s@%s.%s::%d-%d-%d % (dtstr, login, dom, choice(tlds), dtint, llen, dlen))

#print(result_data)

#test re
re_patt = ‘^(\w{3}).*::(?P<email>\w+@\w+.\w+)::(?P<number>\d+-\d+-\d+)‘
for item in result_data:
    m = re.match(re_patt, item)
    if m is not None:
        print(‘*‘*30)
        print(item)
        print("Email: " + m.group(‘email‘))
        print(‘Number: ‘ + m.group(‘number‘)) 
******************************
Tue Jan 28 15:34:09 1992::[email protected]::696584049-7-11
Email: [email protected]
Number: 696584049-7-11
******************************
Thu Dec 23 22:35:52 1971::[email protected]::62346952-6-7
Email: [email protected]
Number: 62346952-6-7
******************************
Sat Jan 25 11:26:50 2003::[email protected]::1043465210-6-8
Email: [email protected]
Number: 1043465210-6-8
******************************
Wed Sep 28 23:37:34 1977::[email protected]::244309054-7-10
Email: [email protected]
Number: 244309054-7-10
******************************
Sun Feb  7 12:08:11 1988::[email protected]::571205291-4-9
Email: [email protected]
Number: 571205291-4-9
******************************
Sat Aug 31 00:04:58 1996::[email protected]::841421098-4-8
Email: [email protected]
Number: 841421098-4-8
******************************
Tue Oct  9 05:32:20 1984::[email protected]::466119140-7-7
Email: [email protected]
Number: 466119140-7-7

Python數據分析學習-re正則表達式模塊