久久国产乱子伦精品免费M,亚洲一区二区三区91,欧美国产在线视频,国产精品视频久久

Python自動化運維 | 正則表達式教學指南

今天寫Python自動化運維工具,偶然想到了初學正則表達式時候,看過一篇文章非常不錯。檢索一下還真的找到了。

re模塊

import re

re.search

經常用match = re.search(pat, str)的形式。因為有可能匹配不到,所以re.search()后面一般用if statement。

str = 'an example word:cat!!'
match = re.search(r'word:\w\w\w', str)
if match:
    print 'found', match.group() ## 'found word:cat' else:
    print 'did not find'
found word:cat
# re.search return a match object, which contains lots of info print type(match)
<type '_sre.SRE_Match'>
print match.string # source string print match.group()
print match.start() # position of w print match.end() # position of t print match.endpos # position of last ! print match.span()
an example word:cat!!
word:cat
11
19
21
(11, 19)

re.match

re.match和re.search很相似,只是re.match是從字符串的開頭開始匹配。

s = 'Python tuts'
match = re.match(r'py',s)
if match:
    print(match.group())
py
s = 'Python tuts'
match = re.search(r'^py',s)
if match:
    print(match.group())
py

常用正則字符意義

  • a, X, 9,等字符匹配自己, 元字符不匹配自己,因為有特殊意義,比如 . ^ $ * + ? { }[ ] \ | ( )
  • . 英文句號,匹配任意字符,不包含'\n'
  • \w 匹配'word'字符,[a-zA-Z0-9]
  • \W 匹配非'word'字符
  • \b 匹配'word'和'non-word'之間邊界
  • \s 匹配單個whitespace字符,space, newline, return, tab, form [\n\r\t\f]
  • \S 匹配non-whitespace字符
  • \t, \n, \r 匹配tab, newline, return
  • \d 匹配數字[0-9]
  • ^ 匹配字符串開頭
  • $ 匹配字符串結尾

重復

‘+’ 一或多次, ‘*’ 零或多次, ‘?’ 零或一次

方括號[]

# 提取email address
string = 'purple [email protected] monkey dishwasher'
match = re.search(r'\w+@\w+', string)
if match:
    print match.group()  ## 'b@google'
b@gmail

[]類似于or
Square brackets can be used to indicate a set of chars, so [abc] matches 'a' or 'b' or 'c'.

match = re.search(r'[\w.-]+@[\w.-]+',string)
if match:
    print match.group()
[email protected]

Group Extraction圓括號()

有時候需要提取匹配字符的一部分,比如剛才的郵箱,我們可能需要其中的username和hostname,這時候可以用()分別把username和hostname包起來,就像r'([\w.-]+)@([\w.-]+)',如果匹配成功,那么pattern不改變,只是可以用match.group(1)和match.group(2)來username和hostname,match.group()結果不變。

string = 'purple [email protected] monkey dishwasher'
match = re.search(r'([\w\.-]+)@([\w\.-]+)',string)
if match:
    # Return subgroup(s) of the match by indices or names. print match.group() # or match.group(0) print match.group(1)
    print match.group(2)
if match:
    # Return a tuple containing all the subgroups of the match, from 1. print match.groups()
[email protected]
alice-b
google.com
('alice-b', 'google.com')

findall and groups

()和findall()結合,如果包括一或多個group,就返回a list of tuples。

str = 'purple [email protected], blah monkey [email protected] blah dishwasher'
tuples = re.findall(r'([\w\.-]+)@([\w\.-]+)', str)
print tuples  # [('alice', 'google.com'), ('bob', 'abc.com')] for tuple in tuples:
    print tuple[0] # username print tuple[1] # host
[('alice', 'google.com'), ('bob', 'abc.com')]
alice
google.com
bob
abc.com

給re.search加^之后是一樣的。

re.sub

re.sub(pat, replacement, str)在str里尋找和pattern匹配的字符串,然后用replacement替換。replacement可以包含\1或者\2來代替相應的group,然后實現局部替換。

# replace hostname
str = '[email protected], and [email protected]' #returns new string with all replacements, # \1 is group(1), \2 group(2) in the replacement print re.sub(r'([\w\.-]+)@([\w\.-]+)', r'\[email protected]', str)
[email protected], and [email protected]

參考

相關新聞

歷經多年發展,已成為國內好評如潮的Linux云計算運維、SRE、Devops、網絡安全、云原生、Go、Python開發專業人才培訓機構!

    1. 主站蜘蛛池模板: 体育| 喀喇| 信宜市| 新龙县| 桂平市| 商城县| 连城县| 集安市| 千阳县| 临沂市| 晋城| 海阳市| 左贡县| 麻城市| 安龙县| 贵港市| 库车县| 阜平县| 颍上县| 黎川县| 湘乡市| 远安县| 临泉县| 临汾市| 吉木乃县| 乐亭县| 石阡县| 武义县| 沙湾县| 马鞍山市| 通渭县| 靖宇县| 云安县| 延安市| 玛纳斯县| 稷山县| 应用必备| 肃北| 贡嘎县| 桂林市| 汤原县|