Re: Regular expression
__all__= [ "Match","Fullmatch","Search","Sub","subn","Split", "FindAll","Finditer","Compile","Purge","Template","Escape", "Error","A","I","L","M","S","X","U", "ASCII","IGNORECASE","LOCALE","MULTILINE","Dotall","VERBOSE", "UNICODE",]
Some constants
I = IGNORECASE = Sre_compile. Sre_flag_ignorecase#Ignore case#make the match case insensitiveL = LOCALE = Sre_compile. Sre_flag_locale#assume current 8-bit locale#affects "W," W, "B, and" B, depending on the current localization setting. Locales is a feature in the C language library and is used to help with programming that requires different languages to consider. #For example, if you are working with French text, you want to use "w+ to match the text, but" W matches only the character class [a-za-z]; it does not match "é" or "?". If your system is properly configured and localized to French, the internal C function tells the program that "é" should also be considered a letter. #The use of the LOCALE flag when compiling regular expressions will give you the ability to use these C functions to process "W" compiled objects, which will be slower, but will also be able to match the French text with "w+" as you would expect. M = MULTILINE = Sre_compile. Sre_flag_multiline#Make anchors look for newline#use "^" to match only the beginning of the string, and $ to match only the end of the string and the end of the string directly before the line break (if any). #when this flag is specified, "^" matches the start of the string and the beginning of each line in the string. Similarly, the $ metacharacters match the end of the string and the end of each line in the string (directly before each line break). S = Dotall = Sre_compile. Sre_flag_dotall#Make dot match newline#make the "." Special character match any character exactly, including line breaks; no this flag, "." matches any characters except line breaks. X = VERBOSE = Sre_compile. Sre_flag_verbose#ignore whitespace and comments#When the flag is specified, a white space character in the re string is ignored, unless the whitespace is in the character class or after the backslash, which allows you to organize and indent the re more clearly. It can also allow you to write comments to the RE, which are ignored by the engine; the comment is identified by the "#" sign, but the symbol cannot be followed by a string or backslash.
Function
Match () matches from scratch, no return null
Search () in the string to return to the first
Pattern =' This'text='Does This text match this pattern?'Match=Re.match (pattern, text) search=Re.search (pattern, text) s=Search.start () e=search.end ()Print(Match)Print(Search.re.pattern)Print(search.string)Print(s)Print(e)Print(Text[s:e])"""nonethisdoes This text match this pattern?59this"""
Complie ()
Regex = Re.compile (pattern)print(regex.match (text))print( Regex.search (text))"" "none<_sre. Sre_match object; Span= (5, 9), match= ' This ' > ' ""
FindAll () and Finditer ()
The iterator generates a match instance and obtains information through group () Start () End ()
Text ='abbaaabbbbaaaabbbbbaaa'pattern='AB'Print(Re.findall (pattern, text)) AB=Re.finditer (pattern, text) forMatchinchAB:Print(Match) forMatchinchAB:Print(Str (Match.start ()) +' -'+ str (match.end ()), end='=') Print(Match.group ())"""[' AB ', ' ab ', ' ab ']<_sre. Sre_match object; span= (0, 2), match= ' AB ' ><_sre. Sre_match object; Span= (5, 7), match= ' AB ' ><_sre. Sre_match object; span=, match= ' ab ' >0->2=ab5->7=ab13->15=ab"""
Groups () All matching strings
Group () integer match string
Group (0) Group (1) string matching by group
Sub () and SUBN ()
SUBN () returns the ancestor, including the number of replacements
Bold = Re.compile (r'\*{2} (. *?) \*{2}') Text="Make this **bold**. This **too**."Print(text)Print(Bold.sub (R'<b>\1</b>', text, Count=1))Print(Bold.subn (R'<b>\1</b>', text))"""Make this **bold**. This **too**. Make this <b>bold</b>. This **too**. (' Make this <b>bold</b>. This **too**. ', 1)"""
Python standard library--re module