Python Full Stack development * 29 Knowledge Point Summary * 180712

Source: Internet
Author: User

29 Regular Expression re-module
I. Regular expressions
Official definition: A regular expression is a logical formula for string manipulation, which is to compose a "rule string" with a predefined set of characters and a combination of those specific characters.
This "rule string" is used to express a filtering logic for a string. Regular expressions
Role:
1. Canonical string rules (a rule is found in a string to match the content of the rule)
2. Determine if a string matches the rule.
(i). character groups [-] from small to Osashi in ASCII order
The various characters that may appear in the same location make up a group of characters, expressed in a regular expression using []
Characters are divided into classes, such as numbers, letters, punctuation, etc.

(ii). Character

Meta-character matching content
. Match any character other than line break
\w match letters or numbers or underscores
\s matches any whitespace character
\d Matching numbers
\ n matches a line break
\ t matches a tab
\b Matches the end of a word
^ Start of matching string
$ matches the end of a string
\w matches non-alphabetic or numeric or underlined
\d matches non-numeric
\s matching non-whitespace characters
A|b match character A or character B
() matches the expression in parentheses, and also represents a group
[...] Match characters in a character group
[^...] Match all characters except characters in a character group
(c). quantifier

Quantifier Usage Description
* Repeat 0 or more times
+ Repeat one or more times
? Repeat 0 or one time
{n} repeats n times
{n,} repeats n or more times
{N,m} repeats n to M times
(iv). Character set character set [][^]
Lee [Jackie Ying two stick]* expression matches "Lee" character after [Jackie Ying two stick] characters any time
Lee [^ and]* indicate that matches a character that is not "and" any time
[\d] 456BDHA3 means matching any number, matching to 4 results
[\d]+ 456bdha3 means matching any number, matching to 2 results
(v). Grouping or | [^]
The ID number is a 15 or 18 character string, and if it is 15 bits all??? Number, the first cannot be 0;
If it is 18 bits, then the first 17 digits are all numbers, the last may be a number or x, below we try to use the regular to represent
^[1-9]\D{16}[0-9X]|^[1-9]\D{14}
(vi) the signifier \
1. In regular expressions, there are a lot of special meanings are metacharacters, such as \d and \s, etc.
If you want to match the normal "\d" instead of "number", you need to escape the "\" to become ' \ \ '
2. So if the match "\d", the string to write ' \\d ', then the regular will be written in "\\\\d", so it is too troublesome.
At this point we use the concept of R ' \d ', where the regular is R ' \\d ' ("R" means to cancel all escapes)
(vii) Greedy match greedy match: matches the string as long as possible when matching matches, by default, greedy match
<.*> The default is greedy match mode, matching the string as long as possible
<.*?> Plus? To convert the greedy match pattern to a non-greedy match pattern, match the shortest possible string
(eight) lazy matching (meta-character + quantifier + "?") Minimum number of Matches
If there is a condition, the condition matches the least, and the character matches any length, it stops immediately when it encounters the identity (crawler needs)
(ix) Decimals
\d+\.? \d+ has flaws (12.)
\d+ (\.\d+)? Remember
Two. Re module
Module: Python way to manipulate a certain content that does exist
Re module: How to use Python to manipulate the regular
1.re.findall ("Regular", "string to match")
2.re.search ("Regular", "with matching string") returns an object assigned to the variable ret; Get a matching string by Ret.group ()
If the string does not match, none is returned.
Import re
Ret=re.findall (' \d+ ', "fhjgh56556jjj87878")
Print (ret) # Returns all results that meet the matching criteria, placed in the list
Print (ret)
Ret=re.search ("\d+", "gdhgjh767676hjhljljj898989")
Print (Ret.group ())

Python Full Stack development * 29 Knowledge Point Summary * 180712

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.