Python (5)-Regular expression

Source: Internet
Author: User

Greedy mode and non-greedy mode of quantitative words

Regular expressions are typically used to find matching strings in text. The number of words in Python is greedy by default (which may be the default non-greedy in a few languages), always trying to match as many characters as possible, and not greedy, instead, always trying to match as few characters as possible. For example: the regular expression "ab*" will find "abbb" if it is used to find "ABBBC". And if you use a non-greedy quantity word "ab*?", you will find "a".

Re.compile ()

>>> exp = re.compile ("[A-z]")     # generates a regular object to match >>> m = Exp.match ('bigtext')       print(M.group ()) b

Matching IP

" 192.168.0.1 "  = Re.match ("([0-9]{1,3}\.) {3}\d{1,3}", String)print(M.group ())

Match phone number

# Match Phone number  "Hey My name is Zhangsan, and my phone number is 13651053467""  Hey my name is Zhangsan, and my phone number is 15811784456"= Re.search ("(1) ([35 87]\D{9})", Phone1)  # The first group matches the first number, the second group matches the second number, and the third group matches the remaining 9 digits

Python (5)-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.