Python built-in module-re, python module-re
To use regular expressions in python, You need to first import the re module. Regular Expressions are a powerful function that can save us a lot of work.
1. metacharacters: Use Symbols with special meanings to indicate characters or positions of a specific type.
.Match any character except linefeed
\ W matches letters, numbers, underscores, or Chinese characters \ W matches any blank characters that match non-letter numbers, underscores, or Chinese characters \ s matches any blank characters \ d matches numbers \ D matches non-digit characters \ B matches start or end of a list ^ matches the start of a string, if it is placed at the beginning of the string, it indicates that it is not. $ Number of end matches of a matching string * zero or multiple times of repetition + one or more times of repetition? Repeat zero or one {n} repeat n times {n,} repeat n times or multiple {n, m} repeat n to m times. The range [] is used to match a specified character category. A character category is a character set that you want to match. It can be understood as an or relation to characters in the character set. [0-9] match 0 ~ 9 digits, match All lowercase letters with \ d [a-z] match all uppercase letters [a-zA-Z] match all letters [A-Z] equivalent to \ w string escape
To match the metacharacters themselves or some special characters in the regular expression, use\Escape. For example, match*This character is used\*, Match\This character, use\\.
Escape characters:$,(,),*,+,.,[,],?,\,^,{,},|
To avoid excessive usage, python provides native character methods, that is, adding an "r" before the string, which indicates that "\" in the string can be directly used for regular expressions, instead of escaping it again. Therefore, it is recommended to add an "r" before the python Regular Expression string.
Ii. Method of re Module
1. match
Re. match ('rule', 'string') matches a string from the beginning, matching a single string. 2. search re. search ('','') matches the string and returns the first matched value. 3. findall re. findall ('','') matches the string in the form of a list.
Returns all. >>> Re. findall ('\ d +', 'dsg2335dhreh54623grh46fdh57 ') ['000000', '000000', '46', '57'] 4. group, groups
a = "123abc456"print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group()print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(0)print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(1)print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(2)print re.search("([0-9]*)([a-z]*)([0-9]*)", a).groups()5. sub (pattern, repl, string, count = 0, flags = 0) is used to replace the matched string.
>>> import re>>> a = 'sfgwg323dgw13'>>> b = re.sub(r'\d+','111',a)>>> b'sfgwg111dgw111'
6. split (pattern, string, maxsplit = 0, flags = 0) groups based on specified matches
content = "'1 - 2 * ((60-30+1*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )'"new_content = re.split('\*', content)# new_content = re.split('\*', content, 1)print new_content
content = "'1 - 2 * ((60-30+1*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )'"new_content = re.split('[\+\-\*\/]+', content)# new_content = re.split('\*', content, 1)print new_content
inpp = '1-2*((60-30 +(-40-5)*(9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2))'inpp = re.sub('\s*','',inpp)new_content = re.split('\(([\+\-\*\/]?\d+[\+\-\*\/]?\d+){1}\)', inpp, 1)print new_content