Python basic syntax-Regular expressions

Source: Internet
Author: User

Overview

A regular expression is a special sequence of characters that is often used to check whether a pattern is matched. The eighth lesson mainly introduces the usage of Python regular expressions from the following aspects.
(1) Use of RE modules
(2) Character matching, quantity representation, boundary representation
(3) High-level usage of regular expressions
(4) Greedy and non-greedy mode

Re module

(a) match (regular expression, string to be matched)
(1) match from left to right, starting from position.
(2) for a regular match check, if the "to match string" matches the "regular expression", the match method returns the matching object, otherwise none is returned

Operation Result:

<_sre.SRE_Match object at 0x02B33FA8>None

(ii) Group () method: Used to return a matching part of a string

import rers = re.match("51cto", "51cto.com")print(rs.group())

Operation Result:

51cto
Character matching, number representation, boundary representation

(i) Single character match

(1). Match any single character except "\ n"

import rers = re.match(".","a")print(rs.group())rs = re.match(".","1")print(rs.group())rs = re.match("...","abc")    #多个字符print(rs.group())rs = re.match(".","\n")        #\n字符print(rs)

Operation Result:

a1abcNone

(2) \s: matches any whitespace character, such as space, tab "\ T", line break "\ n"

import rers = re.match("\s","\t")print(rs)rs = re.match("\s","\n")print(rs)rs = re.match("\s"," ")print(rs)

(3) \s: matches any non-null character; contrary to \s mode

rs = re.match("\S","\t")print(rs)rs = re.match("\S","abc")  #匹配单个字符,从起始位置print(rs.group())

Operation Result:

Nonea

Other single-character match usages are not listed.
(ii) Quantity representation

(1) * Number of occurrences n >= 0

import rers = re.match("1\d*", "1234567")     #起始是1,后面int出现任意次print(rs.group())rs = re.match("1\d*", "1234567abc")     #起始是1,后面int出现任意次print(rs.group())

Operation Result:

12345671234567

(iii) Border representation
(1) string and Word boundary
(2) Matching grouping

Advanced usage of regular expressions greedy and non-greedy pattern work problems

Milk Tea Pavilion Price settlement system optimization:
1. Use regular expressions to determine if the phone number entered by the customer matches the phone number setting rule:
1) Start with the number 1
2) The second digit is any number in the 3578
3) The remaining 9 bits are 0-9 arbitrary numbers
2, the output mobile phone number operator, the judgment rule:
? Mobile operator: Mobile phone number The first three digits are 134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 178, 182, 183, 184, 187, 188
? Unicom Carrier: Mobile phone number of the first three bits 130, 131, 132, 155, 156, 176, 185, 186
? Telecom Carrier: Mobile phone number The first three bits are 133, 153, 173, 177, 180, 181, 189, 199

3, using regular expression to determine the input milk tea number, if not in the 1-5 range, output: woops! We only sell the above five kinds of milk tea Oh! New flavors to look forward to! and set the judgment procedure to a method in the class.

Python basic syntax-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.