One, what is re
The Python re module (Regular expression regex) provides matching operations for a variety of regular expressions, using this Python-embedded language tool, although not all complex matches can be met. But enough, in most cases, to effectively analyze complex strings and extract relevant information. Python translates the regular expression into bytecode and uses the C language matching engine for depth-first matching.
Second, regular expression syntax
As shown in the table
Some of the special uses are as follows
Three, the main function of RE
Python's re regular expression module defines a series of functions, constants, and exceptions, while regular expressions are compiled into ' regexobject ' instances, which themselves can provide methods for different operations. Next, briefly describe the functions and usage of these functions.
The attributes contained in the 1,re
>>>ImportRe>>>Dir (re) [' DEBUG ',' Dotall ',' I ',' IGNORECASE ',' L ',' LOCALE ',' M ',' MULTILINE ',' S ',' Scanner ',' T ',' TEMPLATE ',' U ',' UNICODE ',' VERBOSE ',' X ',' _maxcache ',' __all__ ',' __builtins__ ',' __doc__ ',' __file__ ',' __name__ ',' __package__ ',' __version__ ',' _alphanum ',' _cache ',' _cache_repl ',' _compile ',' _compile_repl ',' _expand ',' _pattern_type ',' _pickle ',' _SUBX ',' Compile ',' Copy_reg ',' ERROR ',' Escape ',' FindAll ',' Finditer ',' Match ',' purge ',' Search ',' Split ',' Sre_compile ',' Sre_parse ',' Sub ',' Subn ',' sys ',' template ']
2, explanation of the main attributes
2.1 Compile function
The explanations in Python help
onfunctioninmodule re:compile(pattern, flags=0) object.
Re.compile (pattern[, flags])
Converts the pattern and identity of regular expressions into regular expression objects for use by the match () and search () functions.
The values of flags are as follows:
\w\W\b\B\s\S\w\W\b\B\d\D\s\S# ’后面的注释
Case:
>>> pattern =‘ab*\d?‘>>> my_pattern = re.compile(pattern,re.S)
2.2,search function
Python help explained
functioninmodulestring, flags=0) stringformatchtomatchobjectorifmatch was found.
Looking for a matching pattern by string, returns a matching object if no match is found
Just return to None
Case:
>>>Re.search (My_pattern,' AB23CD ') <_sre. Sre_match Object at0xb714e9f8>>>>Re.search (My_pattern,' 23CD ')>>>Or>>>M=re.search (My_pattern,' AB23CD ')>>>M.group (0)' AB2 '>>>Mm=re.search (My_pattern,' 23CD ')>>>Mm.group (0) Traceback (most recent): File"<stdin>", line1,inch<module>attributeerror:' Nonetype 'object has no attribute' Group '
2.3,match
The explanations in Python help
functionmatchinmodule re:matchstring, flags=0) toofstringmatchobjectorifmatch was found.
The match () function attempts to match the regular expression only at the beginning of the string, that is, only the match that starts at position 0 is reported, and the search () function scans the entire string to find a match. If you want to search the entire string for a match, you should use Search ().
Case:
>>> m = re.match(r"ni*",‘niiiiiiiihsomrf‘)>>> m.group(0)‘niiiiiiii‘>>> m = re.match(r"ni*?",‘niiiiiiiisomrf‘)>>> m.group(0)‘n‘>>>
2.4,spilt
Explanation of Python Help
Help on function split in module re:
splitstring, maxsplit=0, flags=0) thestringbytheoftheathe resulting substrings.
Splits the source string at a matching regular expression and returns the segmented string as a list
Case:
>>> re.split(r‘a*\d+‘,‘a333defa32ade3a24‘)[‘‘‘def‘‘ade‘‘‘‘‘]
2.5,findall
Python help explained
HelpOn function findall in module re:FindAll (Pattern,string, flags=0) ReturnaList ofAll non-overlapping matchesinch the string. If One orMore groups is presentinch thePatternreturn aList ofGroups This would beaList ofTuplesif thePattern has more than OneGroup. Empty matches is includedinch the result.
Finds all substrings that match the regular expression in the string and makes up a list to return
Case:
>>>Re.findall (R ' ^ab+/w\d*? ',' Ab12frdabd12ab23adl3 ')[]>>>Re.findall (R ' ^ab+\d*? ',' Ab12frdabd12ab23adl3 ')[' AB ']>>>Re.findall (R ' ^ab\d*? ',' Ab12frdabd12ab23adl3 ')[' AB ']>>>Re.findall (R ' ab\d*? ',' Ab12frdabd12ab23adl3 ')[' AB ',' AB ',' AB ']>>>Re.findall (R ' ab\d* ',' Ab12frdabd12ab23adl3 ')[' AB12 ',' AB ',' Ab23 ']
2.6 Finditer
Explanation of Python Help
help On function finditer in module re : finditer (Pattern, string , flags=< Span class= "Hljs-number" >0 ) Return an iterator over all non-overlapping MATC Hes in the String . For each match, the iterator returns a match object. Empty matches is included in the result .
Similar to FindAll, finds all substrings that match the regular expression in the string and makes up an iterator to return.
2.7 Sub
Explanation of Python Help
Help onfunction SubinchModule re:sub (Pattern, REPL,string,Count=0, flags=0) Return the stringObtained byReplacing theLeftmost non-overlapping occurrences of thePatterninch string by theReplacement REPL. REPL can either astring ora callable;ifAstring, Backslash escapesinch itis processed. Ifit isA callable,it' s passed theMatch Object andMustreturnA replacementstring tobe used.
Finds all substrings matching the regular expression pattern in string strings and replaces them with another string repl. If no string matching the pattern is found, a string that has not been modified is returned. Repl can be either a string or a function.
2.8 SUBN and sub functions are similar
can refer here https://www.ibm.com/developerworks/cn/opensource/os-cn-pythonre/
Python Regular expression re template