--- --- restore content begins
Python Regular Expressions
Regular expressions are a special character sequence, it can help you to easily check whether a string matches a pattern.
re.match function
re.match attempts string from the starting position to match a pattern matching the start position if not successful, match () returns none.
grammar:
re.match(pattern, string, flags=0)
pattern ---> matching regular expression
string ---> string to match
flags ---> flags to control the regular expression matching mode
re.search method
re.search scan the entire string and returns the first successful match.
grammar:
re.match(pattern,string, flags=0)
pattern ---> matching regular expression
string ---> string to match
flags ---> flags to control the regular expression matching mode
Search and replace
re.sub(pattern,rep1,string,count=0,flags=0)
parameter:
- pattern: in a regular pattern string.
- repl: replace the string, it may be a function.
- string: find the original string to be replaced.
- count: Maximum number of replacements of the pattern matching, default 0 means to replace all occurrences.
re.compile function
function is used to compile a regular expression compiler generates a regular expression (the Pattern) object for match () and search () function uses these two.
re.compile(pattern[,flags])
parameter:
-
pattern: a string of regular expression
-
the flags: optional, represent the matching pattern, such as ignoring the case, multi-line mode, for the specific parameters:
- re.I ignore case
- re.L represent special characters \ w, \ W, \ b , \ B, \ s, \ S depends on the current environment
- re.M multiline mode
- re.S that is . and include any character, including newline ( not included newline)
- re.U represent special characters \ w, \ W, \ b , \ B, \ d, \ D, \ s, \ S depends on the Unicode character properties database
- re.X To increase readability, ignoring whitespace and comments back #
findall
Find expression in the string being matched by all the sub-strings , and returns a list , if no match is found, it returns an empty list.
Note: match and search is a match findall match all.
findall(string[, pos[, endpos]])
Output:
['123', '456'] ['88', '12']
re.finditer
And findall Similarly, the string is found in the positive expression matched all substrings, and returns them as an iterator.
re.finditer(pattern, string, flags=0)
Output:
12
32
43 3
. | Matches any single character except "\ n" is. To match include '\ n', including any character, like the use of '[. \ N]' mode. |
\d | Matches a digit character. Equivalent to [0-9]. |
\D | Matching a non-numeric characters. It is equivalent to [^ 0-9]. |
\s | Matches any whitespace characters, including spaces, tabs, page breaks, and so on. Is equivalent to [\ f \ n \ r \ t \ v]. |
\S | 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。 |
\w | 匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。 |
\W | 匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'。 |
[Pp]ython | 匹配 "Python" 或 "python" |
rub[ye] | 匹配 "ruby" 或 "rube" |
[aeiou] | 匹配中括号内的任意一个字母 |
[0-9] | 匹配任何数字。类似于 [0123456789] |
[a-z] | 匹配任何小写字母 |
[A-Z] | 匹配任何大写字母 |
[a-zA-Z0-9] | 匹配任何字母及数字 |
[^aeiou] | 除了aeiou字母以外的所有字符 |
[^0-9] | 匹配除了数字外的字符 |