Python basic-re Regular module

Source: Internet
Author: User

First, Introduction:
Regular Expressions: is a small, highly specialized programming language (in Python) that is embedded in Python and implemented through the RE module, and the regular expression pattern is compiled into
a sequence of bytecode, which is then executed by a matching engine written in C.

Second, character match (ordinary character, meta character)
Normal characters:
Meta-character:. ^ $ * + ? { } [ ] | ( ) \

\ followed by meta character removal of special functions
\ Back with ordinary characters for special functions

\d matches any decimal number; equivalent to [0-9]
\d matches any non-numeric character; equivalent to [^0-9]
\s matches any whitespace character; equivalent to [\t\n\r\f\v]
\s matches any non-whitespace character; equivalent to [^ \t\n\r\f\v]
\w matches any alphanumeric character; equivalent to [a-za-z0-9_]
\w matches any non-alphanumeric character; equivalent to [^a-za-z0-9_]
\b Matches a word boundary, such as a space, &, #等

Importre matches the first satisfied resultPrint(Re.search ('Fansik','Fansik,,,,, Fansik'). Group ()) match phone number:Print(Re.findall ('^0?1[3|4|5|8][0-9]\d{8}$','13141466177') ) to group namesPrint(Re.search ('(? P<ID>\D{3})/(? P<NAME>\W{3})','ASFDA521/DFDSFAA'). Group ('ID'))Print(Re.search ('(? P<ID>\D{3})/(? P<NAME>\W{3})','ASFDA521/DFDSFAA'). Group ('name') cancels the priority of the groupPrint(Re.findall ('www. (\w+). com','www.baidu.com'))#Execution Result:['Baidu']
Print(Re.findall ('www. (?:\ w+). com','www.baidu.com'))#execution result after priority cancellation:['www.baidu.com']

Methods of regular Expressions:
FindAll (): All results are returned to a list
Search (): Returns the first object that is matched to (object), which can call the group () to return a result
Match (): matches only at string start match
Print (Re.match (' Fansik ', ' Fansikfanjinbao '). Group ())
Split (): Splits a string with a specified character
Print(Re.split ('Ma','Fansikmafanjinbaomazhansanmalisi') ) Execution Result: ['Fansik','Fanjinbao','Zhansan','Lisi']re.sub (): Character substitutionPrint(Re.sub ('Ma','DF','Fansikmafanjinbaomazhansanmalisi') Execution Result: Fansikdffanjinbaodfzhansandflisire.comile (): Compiles a rule that uses a rule that is used repeatedly with the case of obj= Re.compile ('\.com')Print(Obj.findall ('fansik.com'))
Common rules for matching
Mobile:/^0?1[3|4|5|8][0-9]\d{8}$/
Fixed:/^0[\d]{2,3}-[\d]{7,8}$/
E-Mail:/^ ([a-z0-9_\.-]+) @ ([\da-z\.-]+) \. ([A-z\.] {2,6}) $/
/^[a-z\d]+ (\.[ a-z\d]+) *@ ([\da-z] (-[\da-z])?) +(\. {A} [a-z]+] +$/
User name:/^[a-z0-9_-]{3,16}$/
Password:/^[a-z0-9_-]{6,18}$/
url:/^ (https?:\ /\/)? ([\da-z\.-]+) \. ([A-z\.] {2,6}) ([\/\w \.-]*) *\/?$/

Python basic-re Regular module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.