Python basic five--regular expression

Source: Internet
Author: User

Regular expressions

First, query function

ImportRes='Besttest is good are'Print(Re.match (R' be', s). Group ())#string pre-plus R for the original string#The match method receives 3 parameters, the first one is the regular expression, the second is the string to look for, the third is not required, and the match is used to control the regular pattern.#is to match the string from the first word if it matches to return an object, otherwise, returns nonePrint(Re.search (R' is', s). Group ())#search is from the entire content, found on the return to the first, not found to return nonePrint(Re.findall (R' is', s))#Find all the stringsPrint(Re.sub (R' is','is very', s))#Replace stringPrint(Re.split (' is', s))#Split String

Second, the number of words

Import RE

1 Print(Re.findall (R'be*e','b be beebest is very OK'))# ^Match * One of the preceding characters appears 0 or more times2 Print(Re.findall (R'st+','Besttest is the best s'))#+ Match previous character 1 or more times, just + one character in front3 Print(Re.findall (R'St?','Besttest is the best s'))#? Match the previous character 0 or 1 times, just? One of the preceding characters4 Print(Re.findall (R'T{2}er','besttest is best letter'))#{n} matches the previous character N times 5 Print(Re.findall (R't{1,3}','Besttest is the best letterv ttt'))#{n,m} matches the previous character N to M times

C. General characters

 Import re

1 Print(Re.findall (R'B.','besttest is best letter'))#. The default match is any character except \ n2 Print(Re.findall (R'\?','BES??? TTest is best letter'))#\ Translator, front of * +? These characters have special meanings, and if you need to find them, you have to translate them.3 Print(Re.findall (R'Best|is','besttest is best'))#| match | left or | right character4 Print(Re.findall (R'E[ras]','besttest is best letter'))#[] Set of character sets, the collection of Capricorn characters, which matches any one of the set.5 Print(Re.findall (R'E[^ras]','besttest is best letter'))#If you use ^ in [], it means that you don't include these strings.

  

Iv. Boundary Matching

 Import re

1 Print(Re.findall (R'^http://','http://www.baidu.com Besttest is good\nbest'))#^ matches start with what character, and multiple lines match the beginning of each line2 Print(Re.findall (R'^b','Besttest is good\nbest', Re. M))#Re. M is multi-line mode3 Print(Re.findall (R'jpge$|png|npg$','Touxiang.png'))#$ matches what character ends and matches the end of each line in multiple lines4 Print(Re.findall (R'\ahttp://','http://www.baidu.com\nhttp://www.souhu.com', Re. M))#\a only begins with what character, and ^ different is that it cannot be used in multiline mode5 Print(Re.findall (R'\.jpge\z|\.png\z|\.npg\z','TOUXIANG.PNG\NYI.NPG', Re. M))#\z Only the end of what character, and the difference is that it cannot be used in multiline mode

V. Predefined character sets

 Import re

1 Print(Re.findall (R'\d','S135434657EHFU243FDSF'))#\d Match Number 0-9 # [A-z] case letter [0-9] number2 Print(Re.findall (R'\d+','S135434657EHFU243FDSF'))3 Print(Re.findall (R'\d','S135434657EHFU243FDSF'))#\d matches non-numeric4 Print(Re.findall (R'\w','s13543465^*[email protected] Hello F'))#\w matches all letters, numbers and Chinese5 Print(Re.findall (R'\w','S13543465^*[email protected]'))#\w matches are not letters and numbers6 Print(Re.findall ('\s','We j\tr \NOE\RHGORHG'))#\s matching whitespace characters: \ t, \ n, \ r, Space7 Print(Re.findall ('\s','We j\tr \NOE\RHGORHG'))#\s matches non-whitespace characters

Six, group matching

#Group Matching#such as matching IP addressPrint(Re.findall (R'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}','192.168.160.3'))#GroupingPrint(Re.search (R'\d{1,3} (\.\d{1,3}) {3}','192.168.160.3'). Group ())Print(Re.findall (R'\d{1,3} (\.\d{1,3}) {3}','192.168.160.3'))#FindAll There are groups, the default result is only the contents of the group, that is, the contents of the parentheses.Print(Re.findall (R'\d{1,3} (?: \. \d{1,3}) {3}','192.168.160.3'))

Python basic five--regular expression

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.