python之re模塊

re模塊

re(正則)簡介

? 正則就是用一些具有特殊含義的符號組合到一起(稱為正則表達式)來描述字符或者字符串的方法。或者說:正則就是用來描述一類事物的規則。

re元字符

元字符匹配內容
\w匹配字母(包含中文)或數字或下劃線
\W匹配非字母(包含中文)或數字或下劃線
\s匹配任意的空白符
\S匹配任意非空白符
\d匹配數字
\D匹配非數字
\A從字符串開頭匹配
\n匹配一個換行符
\t匹配一個制表符
^匹配字符串的開始
$匹配字符串的結尾
.匹配任意字符,除了換行符,當re.DOTALL標記被指定時,則可以匹配包括換行符的任意字符。
[...]匹配字符組中的字符
[^]匹配除了字符組中的字符的所有字符
*匹配0個或者多個左邊的字符。
+匹配一個或者多個左邊的字符。
匹配0個或者1個左邊的字符,非貪婪方式。
{n}精準匹配n個前面的表達式。
{n,m}匹配n到m次由前面的正則表達式定義的片段,貪婪方式
a|b匹配a或者b
()匹配括號內的表達式,也表示一個組
s = "meet 黑哥_dsb"
print(re.findall("\w",s))
# 結果:['m', 'e', 'e', 't', '黑', '哥', '_', 'd', 's', 'b']
s = "meet @黑哥!_dsb"
print(re.findall("\W",s))
# 結果:[' ', '@', '!']
s = "meet 黑哥\n_dsb\t"
print(re.findall("\s",s))
# 結果:[' ', '\n', '\t']
s = "meet 黑哥\n_dsb\t"
print(re.findall("\S",s))
# 結果:['m', 'e', 'e', 't', '黑', '哥', '_', 'd', 's', 'b']
s = "meet1 黑哥2_dsb3"
print(re.findall("\d",s))
# 結果:['1', '2', '3']
s = "meet1 黑哥2_dsb3"
print(re.findall("\D",s))
# 結果:['m', 'e', 'e', 't', ' ', '黑', '哥', '_', 'd', 's', 'b']
s = "meet 黑哥_dsb"
print(re.findall("\Am",s))
print(re.findall("\Ad",s))
# 結果:
['m']
[]
s = "meet \n黑哥\t_\ndsb"
print(re.findall("\n",s))
# 結果:
['\n', '\n']
s = "meet \n黑哥\t_\ndsb"
print(re.findall("\t",s))
# 結果:
['\t']
s = "meet 黑哥_dsb"
print(re.findall("^m",s))
print(re.findall("^d",s))
# 結果:
['m']
[]
s = "meet 黑哥_dsb"
print(re.findall("b$",s))
print(re.findall("sb$",s))
# 結果:
['b']
['sb']
s = "meet 黑哥_dsb"
print(re.findall("m..",s))
# 結果:['mee']
s = "meet1 黑哥2_dsb3"
print(re.findall("[1-3]",s))
# 結果:['1', '2', '3']
s = "meet1 黑哥2_dsb3"
print(re.findall("[^(1-3)]",s))
# 結果:['m', 'e', 'e', 't', ' ', '黑', '哥', '_', 'd', 's', 'b']
s = "meet 黑m哥_dsb meet meee"
print(re.findall("me*",s))
# 結果:['mee', 'm', 'mee', 'meee']
s = "meet 黑m哥_dsb meet meee"
print(re.findall("me+",s))
# 結果:['mee', 'mee', 'meee']
s = "meet 黑m哥_dsb meet meee"
print(re.findall("me*?",s))
print(re.findall("me+?",s))
# 結果:
['m', 'm', 'm', 'm']
['me', 'me', 'me']
s = "meet 黑m哥_dsb meet meee"
print(re.findall("e{3}",s))
# 結果:
['eee']
s = "meet 黑m哥_dsb meet meee"
print(re.findall("e{1,3}",s))
# 結果:
['ee', 'ee', 'eee']
s = "2019-7-26 20:30:30"
print(re.split(":|-|\s",s))
# 結果:
['2019', '7', '26', '20', '30', '30']
s = "meet 黑m哥_dsb meet meee"
print(re.findall("m(.*?)t",s))
# 結果:
['ee', '哥_dsb mee']

re模塊常用方法

  • findall 全部找到返回一個列表

  • search 從字符串中任意位置進行匹配查找到一個就停止了,返回的是一個對象. 獲取匹配的內容必須使用.group()進行獲取

    import re
    print(re.search("sb|nb","alexsb meetnb"))
    print(re.search("sb|nb","alexsb meetnb").group())
    # 結果
    <_sre.SRE_Match object; span=(4, 6), match='sb'>
    sb
  • match 從字符串開始位置進行匹配

    import re
    print()re.match("sb|nb","alexdsb,alex_sb,alexnb,al_ex")
    print(re.match("sb|nb","alexdsb,alex_sb,alexnb,al_ex").group())
    # 結果:
    None
    AttributeError: 'NoneType' object has no attribute 'group'  # 'NoneType'對象沒有屬性'group'
  • split 分隔 可按照任意分隔符進行分隔

    import re
    s = "2019-7-26 20:30:30"
    print(re.split(":|-|\s",s))
    # 結果:
    ['2019', '7', '26', '20', '30', '30']
  • sub 替換

    import re
    s = "meet是一位好老師,meet教會了我們很多知識"
    print(re.sub("meet","蒼老師",s))
    # 結果:
    蒼老師是一位好老師,蒼老師教會了我們很多知識
  • compile 定義匹配規則

    import re
    fn = "\d+"
    s = "太白123meet456"
    print(re.split(fn,s))
    # 結果:
    ['太白', 'meet', '']
  • finditer 返回一個迭代器

    import re
    s = "太白123"
    g = re.finditer("\w",s)
    for i in g:print(i)print(next(i))
    # 結果:
    <_sre.SRE_Match object; span=(0, 1), match='太'>
    太
    <_sre.SRE_Match object; span=(1, 2), match='白'>
    白
    <_sre.SRE_Match object; span=(2, 3), match='1'>
    1
    <_sre.SRE_Match object; span=(3, 4), match='2'>
    2
    <_sre.SRE_Match object; span=(4, 5), match='3'>
    3
  • 給分組起名字

    import re
    ret = re.search("<(?P<tag_name>\w+)>\w+</\w+>","<h1>hello</h1>")  # 給分組1取名tag_name
    print(ret.group("tag_name"))
    print(ret.group())
    # 結果:
    h1
    <h1>hello</h1>import re
    ret = re.search(r"<(\w+)>\w+</\1>","<h1>hello</h1>")   # \1填充分組1中的內容
    print(ret.group(1))
    print(ret.group())
    # 結果:
    h1
    <h1>hello</h1>

轉載于:https://www.cnblogs.com/lifangzheng/p/11264627.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/247335.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/247335.shtml
英文地址,請注明出處:http://en.pswp.cn/news/247335.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Angular自學筆記(?)ViewChild和ViewChildren

ViewChild 最好在ngAfterViewInit之后,獲取模版上的內容 獲取普通dom import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from @angular/core;@Component({selector: app-view-child

IPropertySet接口

Members AllProperties MethodsDescriptionCountThe number of properties contained in the property set.包含屬性個數GetAllPropertiesThe name and value of all the properties in the property set.GetPropertiesThe values of the specified properties.GetPropertyThe …

Angular自學筆記(?)ContentChild和ContentChildren

ContentChild 用法類似ViewChild, 獲取投影中到組件或指令還有元素dom等 獲取投影中但組件 import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from @angular/core;@Component({selector: app-content-child-panel,templateUrl

Angular自學筆記(?)屬性型指令

基本概念 用于改變DOM元素的外觀或行為的指令 組件是一種特殊的指令 import {Component} from @angular/core; @Component({selector: app-root,template: `<!--<app-for></app-for>--><div app-for>dasfsada</div>`,

SNS編年史

準備起草。轉載于:https://www.cnblogs.com/cmleung/archive/2009/11/26/1611546.html

Angular自學筆記(?)結構型指令

內置指令的展開寫法 ngIf import {Component } from @angular/core; @Component({selector: app-root,template: `<button (click)="show = !show">toggle</button><p *ngIf="show as aa">一段文字 {{ aa }}</p><ng-template…

SQL on and 和 on where 的區別

on and 和 on where 的 區別 在使用 left join 時, on and 和 on where 會有區別&#xff1b;1. on的條件是在連接生成臨時表時使用的條件,以左表為基準 ,不管on中的條件真否,都會返回左表中的記錄  on 后面 and 都是對右表進行篩選 2.where是全部連接完后&#xff0c;對臨時…

:host :host-context ::ng-deep詳解

:host 與 ::ng-deep :host 表示選擇當前的組件。 ::ng-deep 可以忽略中間className的嵌套層級關系。直接找到你要修改的className。 在使用一些第三方的組件的時候&#xff0c;要修改組件的樣式。 這種情況下使用: :host ::ng-deep .className{新的樣式...... } :host {backg…

Java生鮮電商平臺-緩存架構實戰

Java生鮮電商平臺-緩存架構實戰 說明&#xff1a;在Java生鮮電商中&#xff0c;緩存起到了非常重要的作用&#xff0c;目前整個項目中才用的是redis做分布式緩存. 緩存集群 緩存集群存在的問題 1.熱key 緩存集群中的某個key瞬間被數萬甚至十萬的并發請求打爆。 2.大value 某個k…

Java生鮮電商平臺-深入理解微服務SpringCloud各個組件的關聯與架構

Java生鮮電商平臺-深入理解微服務SpringCloud各個組件的關聯與架構 概述 毫無疑問&#xff0c;Spring Cloud是目前微服務架構領域的翹楚&#xff0c;無數的書籍博客都在講解這個技術。不過大多數講解還停留在對Spring Cloud功能使用的層面&#xff0c;其底層的很多原理&#xf…

Angular自學筆記(?)DI提供者

類提供者 類提供者的創建和使用 假設有logger類: import {Injectable } from @angular/core;@Injectable() export class LoggerService {logs: string[] = [

Angular自學筆記(?)生命周期

從實例化組件,渲染組件模板時,各聲明周期就已開始 ngOnChanges 輸入屬性發生變化是觸發,但組件內部改變輸入屬性是不會觸發的 import {Component, Input, OnInit, OnChanges } from @angular/core;@Component({selector: app-life-cycle,templateUrl:

[轉載]httpClient.execute拋Connection to refused異常問題

在4.0之后android采用了嚴格模式&#xff1a;所以在你得activity創建的時候&#xff0c;在super.onCreate(savedInstanceState);后面加上這個 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites(…

Angular自學筆記(?)依賴注入

什么是依賴注入 依賴注入(DI)是一種設計模式, 也有相應的框架,比如InversifyJS Angular 有自己的 DI 框架, DI 框架會在實例化該類時向其提供這個類所聲明的依賴項 帶修飾符的參數 在ts中,一個類的參數如果帶上修飾符,那個參數就變成了類的實例屬性 class Mobile {co…

MSN8.0經常出現連接錯誤,如何解決?

連接錯誤有很多種情形&#xff0c;請您先查看下連接錯誤代碼 然后可以嘗試以下解決辦法--------- 如何解決錯誤 81000301 或 81000306 您登錄 MSN Messenger 時&#xff0c;可能會收到以下錯誤消息&#xff1a; 我們無法讓您登錄到 MSN Messenger&#xff0c;可能是因為服務或 …

@ViewChild 的三種常用方法

//--1------ 在angular中進行dom操作 <div #dom>這是一個div</div> //放置一個錨點domimport { ElementRef, ViewChild } from angular/core;ViewChild(dom,{static:true}), eleRef:ElementRef; //static-True表示在運行更改檢測之前解析查詢結果&#xff0c;false…

SQL Server安裝文件掛起錯誤解決辦法

以前在安裝sql的時候&#xff0c;如此提示&#xff0c;我只要重新啟動即可&#xff0c;可是今天重新啟動了N次計算機&#xff0c;問題卻絲毫沒有解決&#xff0c;依然提示這樣的話。“以前的某個程序安裝已在安裝計算機上創建掛起的文件操作。運行安裝程序之前必須重新啟動計算…

angular 內容投影

app HTML <div class"wrapper"><h2>我是父組件</h2><div>這個div定義在父組件中</div><app-child><div class"header">這個div是父組件投影到子組件的1, {{title}}</div><div class"footer"…

移動端日歷插件

//datePicker日期控件 v1.0//var calendar new datePicker();//calendar.init({// trigger: #demo1, /*選擇器&#xff0c;觸發彈出插件*/// type: date,/*date 調出日期選擇 datetime 調出日期時間選擇 time 調出時間選擇 ym 調出年月選擇*/// minDate:1900-1-1,/*最小日期*/…

js 操作location URL對象進行操作

把location 創建URL對象 構造器 new URL() 創建并返回一個URL對象&#xff0c;該URL對象引用使用絕對URL字符串&#xff0c;相對URL字符串和基本URL字符串指定的URL。 屬性 hash 包含#的USVString&#xff0c;后跟URL的片段標識符。 host 一個USVString&#xff0c;其中…