python-Regular expression Syntax specification and case

Source: Internet
Author: User

Regular expressions for use with case studies

2018-08-24 21:26:14

"description": This article is mainly for the subsequent review and use for reference, due to the word comparison, so there is no layout of the blog, no code, to insert the picture-based,

  • The special character of the regular expression
  • Attention:

    In the following case, match () is matched, and match is required to start with the first character, so the front is there. *

  • "1" ^ function-results with the beginning of B matches
  • "2" $ effect-any opening, ending with 3
  • Note: This is not possible below, if there is no * number, it does not mean many times.

  • Indicates that 4 is the third-digit match. The point is matched to the second bit.

  • "3"? The role
  • (1) Greedy match default is reverse matching

    Greedy match, is to start from the back to match. The following example.

  • (2), add one to the left? The number is positive match
  • Add one to the left? , the match will start from left to right. is a non-greedy match, but the parentheses are still a greedy match. "That is, from the left, the first B is fixed, but the second B in the parentheses is the right one," he added. * Then output everything in the middle. 】

  • (3), parentheses are added in parentheses for non-greedy matching
  • # tip: There's a greeting in parentheses, so the first one  OK next . *?b is from left to right
    the output is: Boooooob
  • The function of "4" and "+"
  • A brief description of the above? The principle

    In fact, the above said from the left from the right to match all is a skill of the rules, in fact, can be completely according to the following three symbols meaning to explain.

    * indicates 0 or more times

    + represents 1 or more times

    ? 0 or 1 times.

  • Like what:

    Line= "BOOOOOOOBBAAA"

    Pattern= ". * (B.*B)" Here are two places. * are greedy match patterns, in parentheses. * "because * is indicated 0/1 times," So match the yellow part of the BB.

  • + the role of:

    + actually, it means 1 . times or multiple matching patterns.

    For example: for the following

    Line= "BOOOOOOOBBAAA"

    Pattern= ". * (B.+B)" Can not match success, because. + is at least 1 elements to match, then the beginning of the end is B, the middle of an element is not the case.

    So for example:

    Line= "BOOOOOOOBABAAA"

    Pattern= ". * (B.+B)"

    it matches the result. Bab

  • "Case Study"

  • (1)
  • (2)

  • (3)

  • "5", the role of {2},

    {1} {2} Indicates the presence of a qualified element at this time.

    Case Analysis:

  • (1)
  • (2)

  • (3)
  • "6", {2,}&&{2,5}
  • 1, {2,} indicates output of two or more times

  • {2,5}

    Y is the same as the top, indicating that the greater than or equal to 2 is less than or equal to 5

  • "7", | Usage of
  • This symbol represents or is a relationship.

  • Use of special parentheses

  • Output boobb123

  • Usage of "8", []
  • "1" [] the first usage means that any one of the values in the brackets matches the matching operation.
  • The cases are as follows:

  • "2" [0-9] the second meaning of brackets, can be expressed in intervals.
  • For example, match phone number:

    Phone_num_pattern= "1[3578][0-9]{9}"

    That can match the phone number of the 13*****/15*****/17*************/18***********, the back nine bits are 0-9 arbitrary, {9} represents 9 digits

  • The third usage of 3 [^1]. Indicates that the value of the brackets is not 1 for all cases matching
  • "4" [] the fourth usage [.] . It means a point.
  • "9", \s (small) indicates that a space character can be matched
  • \s is a matching whitespace character

  • "10", \s (Large) is a match for any non-whitespace character except newline
  • If you have more than one character in between you can use "(You \s+ Good)" To do a match to complete

  • "11", the use of \w "focus"------is similar to \s (large)
  • The meaning of \w is that [a-za-z0-9_] is different from \s, such as \s can match to the + number

  • "12", \w is all characters except [a-za-z0-9_]
  • \w is a matching feature that represents all characters except [a-za-z0-9_], which happens to be the opposite of \w.

  • "13", the extraction of Unicode characters
  • The extraction code is [\u4e00-\u9fa5]+

  • "14", understanding the greedy match again
  • The use of the "15" \d is to extract the numbers
  • "16", () represents a group.
  • Summary: Regular expression Summary table
  • Summary case: "Classic case"

  • Case one:

  • Case 2:

python-Regular expression Syntax specification and case

Related Article

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.