Objective:
My environment windows 7 -bit,python2.7
What is re:
Regular expression abbreviation, meaning regular expressions, is One of the many modules of python
Re uses:
Extract the desired text fragment from the selected batch in the text
Re type:
Divided into DFA ( deterministic with poor state automata ) and NFA ( indeterminate with poor state automata )
Re installation:
Open DOS;CD to pip directory; Enter command pip install re
Re common methods:
1.re.compile (pattern, flags=0)
Pattern is of type str, example:pattern = r' ^.*?$ '
2.re.findall (Pattern, string, flags=0)
Return a list of all non-overlapping matches in the string. Returns a list of all non-overlapping strings in a string .
Example 1:print Re.findall (R' (s) (d) ', ' GSD SD FSA GGH SD HF SDGF ')
Results: [(' s ', ' d '), (' s ', ' d '), (' s ', ' d '), (' s ', ' d ')]
Example 2:print Re.findall (R ' (s) d ', ' GSD SD FSA GGH SD HF SDGF ')
Result: [' s ', ' s ', ' s ', ' s ']
Example 3:print re.findall (R ' SD ', ' GSD SD FSA GGH SD HF SDGF ')
Result: [' SD ', ' SD ', ' SD ', ' SD ']
Purpose: Extract links in the source code of the Web page, etc.
3.re.split (Pattern, String, maxsplit=0, flags=0)
Split the source string by the occurrences of the pattern,
Returning a list containing the resulting substrings. Back to List
Example: Print Re.split (R ' s ', ' JSJKJOISHIOSHUISGUUSNJSHBSG ')
Results: [' j ', ' Jkjoi ', ' hio ', ' hui ', ' guu ', ' NJ ', ' HB ', ' G ']
Purpose: Divide large pieces of text into small pieces that are easy to handle
4.re.match (Pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning
A match object, or None if no match was found. Returns an object similar to a pointer
5.re.search (Pattern, string, flags=0)
Scan through string looking for a match to the pattern, returning
A match object, or None if no match was found. Returns an object similar to a pointer
Reference: the IDLE help command;
PostScript: If there are errors and omissions, welcome correction, there is time to update
This article is Bo Master original, reproduced please indicate the source, and @ My heart fly 2015, thank you for your cooperation!
Python Re Module summary