Python: Regular expression re

Source: Internet
Author: User

#re正则的用法: Match match matches from the beginning  search takes one back, findout take so match, slit split  Sub Replace

#-*-Coding:utf8-*-#Auth:fulimei#re regular usage: match match matches from the beginning search takes one back, findout fetch so match, slit split sub ReplaceImportReres=re.match ('^chen','Chenronghua')#^ Match character startResa=res.group ()#matching characters are printed outPrint(RES)Print(RESA)#If you write it dead , you don't have to.#\d matches a number, \d+ matches one or more digitsRe.search#Search this uses the mostA=re.search ('R[a-z]+a','112ronghua44ronghua')#[A-z] take A to Z of a character, more to match the last to take the firstPrint(a)#span= (3, ten), match= ' Ronghua ' >B=re.search ('R[a-za-z]+a','112ronaghua44ronghua')Print(b)#span= (3, one), match= ' Ronaghua ' >#match the previous character 0 or 1 times? Usage ofC=re.search ('AAL?','AADBD')Print(c)#L can not, matched 0 times match= ' AA 'D=re.search ('AAL?','AALDBD')Print(d)#matched 10 times match= ' AAL 'F=re.search ('[0-9]{3}','a1bc234')#Match 3 digits match= ' 234 ' >Print(f)#match one to three times, take one and returnG=re.search ('[0-9]{1,3}','a1bc234')#take one and return to match= ' 1 ' >Print(g)#match all, note findall No group methodH=re.findall ('[0-9]{1,3}','A1B2C345DE')Print(h)#[' 1 ', ' 2 ', ' 345 ']L=re.search ('abc| ABC','ABCDABC')#match= ' ABC 'Print(l) J=re.findall ('abc| ABC','ABCDABC')#match= ' ABC 'Print(J)#[' abc ', ' abc ']K=re.search ('abc{2}','FLMABCCC')Print(k)#match= ' ABCC ' >#match two pipe characters two times as | | =M=re.search ('(\|\|=) {2}','abc| | =|| =')Print(m)#match= ' | | =|| = '#\a match from the last \zN=re.search ('\a[0-9]+[a-z]\z','123b')#the end must be a lowercase letterPrint(n)#match= ' 123b '#\d matches non-numericO=re.search ('\d+','123$%a\r\n')Print(o)#match= ' $%a\r\n ' >#match numbers + lettersP=re.search ('\w+','12a3b$%')Print(p)#match= ' 12a3b ' >#Match Special charactersQ=re.search ('\w+','12a3b$%')Print(q)#match= ' $% ' >#\s Match space wrap tab (\ t)R=re.search ('\s+','123$%a \ r \ n')Print(R)#' (? P<name>, ...) ' Group matching provinces and countiesS=re.search ('(? P<province>[0-9]{3}) (? P<city>[0-9]{3}) (? P<BIRTHDAY>[0-9]{4})','220723198901021625'). Groupdict ()Print(s)Print(s[' City'])#divide split by whatT=re.split ('[0-9]+','ABC12DF44GG456DF')Print(t)#[' abc ', ' DF ', ' GG ', ' DF ']#Sub Substitution replace all numbers with |Y=re.sub ('[0-9]+','|','A1w234fg2ff2gg')Print(y)#a|w|fg|ff| GGZ=re.sub ('[0-9]+','|','A1w234fg2ff2gg', count=2)#Replace the top 2Print(z)#A|w|fg2ff2gg
the use of re regularization


# \s Match Space wrap tab (\ t)

Python: Regular expression re

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.