程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python notes 23- regular expressions

編輯:Python

One 、 Overview of regular expressions

Regular expressions are special sequences of characters , Often used for retrieval 、 Replace a pattern ( Having certain rules ) Some of the text

Registered app, Set the username 、 There are rules for setting passwords

Regular expressions can be used in almost all programming languages , Regular expressions have their own independent syntax and processing engine . For some operations of string, if the native string can complete , That is to use the original , Because native processing is more efficient than regular expressions

Native ones can't be handled or are rather verbose , You can use regular expressions

Python Provides modules for processing expressions , The module is called re

Two 、 Usage scenarios of regular expressions

Inspection operation : Like username 、 password 、 cell-phone number 、 Verification of mailbox, etc

Search query : To query the content in the specified format in a piece of text , Such as extracting the mobile phone number

3、 ... and 、 Regular grammar

explain :
If the character to be matched is a special character in a regular expression , Then you can use \ Escapes , For example, if you want to match the decimal point, you can write . That's all right. , Because write directly . Will match any character ; Empathy , To match parentheses, you must write ( and ), Otherwise, parentheses are treated as groupings in regular expressions .

Four 、Python Support for regular expressions

Python Provides re Module to support regular expression related operations , Here is re Core functions in modules .

explain :
As mentioned above re These functions in the module , Regular expression objects can also be used in practical development (Pattern object ) Instead of using these functions , If a regular expression needs to be used repeatedly , Then go through compile Function to compile regular expressions and create regular expression objects is undoubtedly a wiser choice .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved