A regular expression is described in the standard text expression sequences, in the text processing using regular expressions to describe a string of text. Its design idea is to use a language descriptive string to define a rule, those who comply with the rules of the string, we consider it a "match", otherwise, the string is not legitimate. Which will not be repeated matching substring match.
1. Expresses its character by character
1. Basic symbols were only match [a character]
str(' ') |
Regular expressions ( '') [ '' has a special meaning \] |
The designated numbers: '1' | With the same [ '1'] |
The letter specified :: 'a' | With the same [ 'a'] |
Special symbols are as specified: specify '-' | Need an escape '\' [ '\ -'] |
[Specify various characters such as extraction]: 'a' or 'b' | '[]' 【 '[From]'】 |
The range represents: a number between 1-3 | '-' [ '[1-3]'] [required] used in conjunction with extraction |
Non-specified characters these characters [non] Such as: Non 'a' and not 'b' |
'^' (Caret) [ '[^ ab &]' [] for an extraction in conjunction with, and a position off the first open bracket character to be effective] |
blank | '$' Can only be placed at the end of [] |
'A number' | '\ D' or '\ w' |
'one letter' | '\w' |
'A character' | '.' (Wildcard) |
'A space' (including whitespace Tab etc.) | ‘\s' |
2. [expansion symbol symbols used in combination with the foundation required, and is only valid for the previous symbol]
str ( '') [wherein the symbols are as a not particularly specified: integers, letters and the like] |
Regular expressions ( '') for a character [k] |
'A non-essential character' [0/1] |
'k?' |
'At least one of a character' | 'k+' |
'N number of a character' | 'k{n}' |
'Nm a certain character' | 'k{n,m}' |
'Any number of a character' | 'k * ‘ |
2. Extraction with regular expression some substrings in
Features | function | Parameter Description | return value |
Whether there is a substring that matches the regular expression character string is determined [The substring starting position is 0, both scratch match]
|
re.match(partten,string,flags=0) |
partten: Regular Expressions string: string being matched |
If the match is successful a match object is returned, otherwise None |
Whether there is a substring that matches the regular expression character string is determined [Substring starting position of the arbitrary starting point is not necessarily 0] |
re.search(partten,string,flags=0) | partten: Regular Expressions string: string being matched |
If the match is successful a match object is returned, otherwise None |
Extraction string matches all right substring | re.findall(partten,string,flags=0) | partten: Regular Expressions string: string being matched |
If the match is successful a non-empty list is returned, otherwise [] |