Python正規表示式,統計分析nginx訪問日誌

2022-07-17 07:48:10 字數 2429 閱讀 7358

目標:

1.正規表示式

2.oop程式設計,統計nginx訪問日誌中不同ip位址出現的次數並排序

1.正規表示式

#

!/usr/bin/env python

#-*- coding: utf-8 -*-

importre#

match

#方法一

pattern1 = re.compile(r'

hello

', re.i)

match = pattern1.match('

hello world')

ifmatch:

print

match.group()

#方法二

m = re.match(r'

hello

', '

hello world.')

print

m.group()

#search

pattern1 = re.compile(r'

world')

match = pattern1.search('

hello, hello world.')

ifmatch:

print

match.group()

#split

pattern1 = re.compile(r'

\d+'

)match = pattern1.split('

one1two2three3')

print

match

for i in

match:

printi#

findall

match = pattern1.findall('

one1two2three3')

print

match

#finditer

match = pattern1.finditer('

one1two2three3')

for i in

match:

print i.group()

•執行**,測試效果

2.oop程式設計,統計nginx訪問日誌中不同ip位址出現的次數並排序

•執行**,測試效果

handetiandemacbook-pro:test xkops$ python test2.py[('

192.168.207.1

', 46), ('

192.168.207.21

', 25), ('

192.168.80.165

', 20), ('

127.0.0.1

', 10)]

正規表示式統計

處理金額 1 9 0 9 d 0 d d 非負整數 正整數 0 0 9 1 9 0 9 正整數 d 0 非正整數 負整數 0 0 9 1 9 0 9 負整數 d 整數 d d 非負浮點數 正浮點數 0 0 9 0 9 1 9 0 9 0 9 1 9 0 9 0 9 0 9 1 9 0 9 正浮點數 ...

正規表示式分析

1.請分析這下面這兩個輸出吧,最好乙個乙個斜槓地分析。system.out.println 8abc8 replaceall 8 8abc system.out.println 8abc8 replaceall 8 8abc 2.為什麼a和b可以有相同的輸出,c和d卻不可以呢?system.out....

python正規表示式元字元 正規表示式

字元 描述將下乙個字元標記為乙個特殊字元 或乙個原義字元 或乙個 向後引用 或乙個八進位制轉義符。例如,n 匹配字元 n n 匹配乙個換行符。序列 匹配 而 則匹配 匹配輸入字串的開始位置。如果設定了 regexp 物件的 multiline 屬性,也匹配 n 或 r 之後的位置。匹配輸入字串的結束...