Python-----Regular Expression Exercises

Source: Internet
Author: User

1 #-*-coding:utf8-*-2 #Import re library file3 ImportRe4  fromReImportfindall,search,s5Secret_code ='HADKFALIFEXXIXXFASDJIFJA134XXLOVEXX23345SDFXXYOUXX8DFSE'6 7 #use an example to match any character, except for line break \ n8 #a = ' xy123 '9 #B = Re.findall (' x ... ', a)Ten #C = Re.findall (' x.. ', a) One #Print B A #Print C -  - #* Use an example to match the previous character 0 or unlimited times the #a = ' xyxy123xx456xxx789 ' - #B = Re.findall (' x* ', a) - #Print B - #[' x ', ' ', ' x ', ' , ', ', ', ', ' xx ', ' ', ' ', ', ' xxx ', ', ', ', '] +  - #use an example to match a previous character 0 or 1 times + #a = ' xy123 ' A #B = Re.findall (' x? ', a) at #Print B -  - #the use of #.* for example greedy algorithm - #B = Re.findall (' xx.*xx ', Secret_code) - #Print Secret_code - #Print B in ## #.*? Examples of Use - #C = re.findall (' xx.*?xx ', Secret_code) to #Print C +  -  the ##使用括号与不使用括号的差别 * #d = Re.findall (' xx (. *?) xx ', Secret_code) $ #Print DPanax Notoginseng #For each in D: - #Print each the #Re. S match includes line break + #s = "' Sdfxxhello A #xxfsdfxxworldxxasdf "" the # + #d = Re.findall (' xx (. *?) xx ', S,re. S) #,re. S - #Print D $  $ #Compare the difference between findall and search - #s2 = ' ASDFXXIXX123XXLOVEXXDFD ' - #f = re.search (' xx (. *?) Xx123xx (. *?) xx ', S2). Group (2) the #Print F - #F2 = Re.findall (' xx (. *?) Xx123xx (. *?) xx ', s2)Wuyi #Print F2[0][1] the  - #Replace the sub with an example Wu #s = ' 123rrrrr123 ' - #output = Re.sub (' 123 (. *?) 123 ', ' 123%d123 '%789,s) About #Print Output $  - #demonstrating the different import methods - #info = findall (' xx (. *?) xx ', secret_code,s) - #For each in info: A #Print each +  the #do not use compile superfluous - #pattern = ' xx (. *?) XX ' $ #New_pattern = Re.compile (pattern,re. S) the #output = Re.findall (New_pattern,secret_code) the #Re1 = Re.findall (' xx (. *?) xx ', Secret_code,re. S) the #Print Output the #Print Re1 -  in #Match Numbers the #a = ' Asdfasf1234567fasd555fas ' the ##b = Re.findall (' \d+ ', a) #[' 1234567 ', ' 555 '] About #B = Re.findall (' (\d+) ', a) the #Print B

Python-----Regular Expression Exercises

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.