Python Learning Journey ——— re-module regular expressions

Source: Internet
Author: User

#重复匹配:. [] ? * + {}

?: The left character appears 0 or 1 times

Print (Re.findall (' ab ', ' a ab ABB abbb abbbbbb '))
Print (Re.findall (' ab{0,1} ', ' A ab ABB abbb abbbbbb '))

*: The character on the left appears 0 or infinite times

Print (Re.findall (' ab* ', ' a ab ABB abbb abbbbbb abbc123bbbb '))
Print (Re.findall (' ab{0,} ', ' A ab ABB abbb abbbbbb abbc123bbbb '))

+: The character on the left appears 1 or infinite times

Re.findall (' ab+ ', ' a ab ABB abbb abbbbbb abbc123bbbb ')
Re.findall (' Ab{1,} ', ' A ab ABB abbb abbbbbb abbc123bbbb ')

Greedy match:. *
Print (Re.findall (' a.*b ', ' a123b456b '))

Non-greedy match:. *?
Print (Re.findall (' a.*?b ', ' a123b456b '))

Group: ()

Print (Re.findall (' <imag href=\ "(. *) \"/> ",
' '
Attention:

The use of regular expressions in Python is first recognized by Python. (That is, it will consume one time first \)

Print (Re.findall (' a\\\\c ', ' a\c A12 a2c ')) # ' a\\c ' so you want to write two more
Print (Re.findall (R ' a\\c ', ' a\c A12 a2c ')) # ' a\\c '

1 #Why the same expression search and findall have different results:2 Print(Re.search ('\ (([\+\-\*\/]*\d+\.? \d*) +\)',"1-12* (60+ ( -40.35/5)-( -4*3))"). Group ())#( -40.35/5)3 Print(Re.findall ('\ (([\+\-\*\/]*\d+\.? \d*) +\)',"1-12* (60+ ( -40.35/5)-( -4*3))"))#['/5 ', ' * * ']4 5 #See this example: (\d) + equivalent (\d) (\d) (\d) (\d) ..., a series of groupings6 Print(Re.search ('(\d) +','123'). Group ())#The role of group is to bring all the groups together to show7 Print(Re.findall ('(\d) +','123'))#FindAll result is a result of the group and is the result of the last group8 3 values were taken for this case9' 1 ' 2 ' 3, respectively.'TenAnd then it shows the number 3.





Python Learning Journey ——— re-module regular expressions

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.