The regular expression, which is simpler, is a matching function, in Python, as long as the re module is referenced, you can do regular matching operations
One, Math match
Let's look at a simple example
Import Rere.match (pattern, data_source) #math为匹配, pattern is the object that needs to be matched, data_source as the data source
View the results of the match
Import rea = Re.match (' abc ', ' ABCDEFG ') b = Re.match (' abc ', ' BCDEFG ') print (a) print (b)-----result-----<_sre. Sre_match object; Span= (0, 3), match= ' abc ' > #a为匹配到的结果, returns a regular object of None #b为没匹配到的结果
To see the specific results that match, you need to use the group method:
Import rea = Re.match (' abc ', ' ABCDEFG ') print (A.group ())-----result-----ABC
It is important to note that math can only match from the beginning of the string
Import rea = Re.match (' abc ', ' AABCDEFG ') if a: print (' match succeeded ') Else: print (' match failed ')-----result-----Match failed
In math, there are several ways to see what matches are in addition to the group method:
Import rea = Re.match (' abc ', ' ABCDEFG ') print (A.start ()) #查看匹配开始的位置print (A.end ())) #查看匹配结束的位置print (A.span ()) #查看匹配开始和结束的位置-----Results-----03 (0, 3)
If you want to match any number, you can write this:
Import rea = Re.match (' [0-9] ', ' 1234abc ') #[0-9] to match any number B = Re.match (' [0-9]{1,9} ', ' 1234abc ') #[0-9]{1,9} To match any 1 to 9 digital print (A.group ()) print (B.group ())-----Results-----11234
Second, search
As explained above, Math,math is only matched from the beginning, and if you want to match from anywhere in the entire string, we can use search:
Import rea = Re.search (' \d ', ' b1234abc123 ') #\d to match any decimal number, equivalent to [0-9]print (A.group ())-----Result-----1
It is important to note that search does not match the content as long as it matches the content.
Third, FindAll
The FindAll method can return a matching substring in the form of a list, as seen in the example:
Import rea = Re.findall (' [0-9] ', ' 1234abc123 ') b = re.findall (' [0-9]{0,9} ', ' 1234abc123 ') c = Re.findall (' [0-9]{1,9} ', ' 1234abc123 ') print (a) print (b) print (c)-----result-----[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 1 ', ' 2 ', ' 3 '] [' 1234 ', ', ', ', ', ' 123 ', ']
#由于 {0,9} is able to match to 0 numbers, so the final result will match to the empty string [' 1234 ', ' 123 '] #改为 {1,0}, it will not match to 0 numbers
Iv. Sub and Subn
In a few of the methods described earlier, you can only see the results of a match, and if we want to replace the match with something else, we could use sub and subn, which is the usual sense of substitution .
Import rea = Re.sub (' A ', ' Z ', ' b1234abc123aaa ') #把a换成zb = re.subn (' A ', ' Z ', ' b1234abc123aaa ') print (a) print (b)----- Results-----b1234zbc123zzz (' b1234zbc123zzz ', 4) #使用subn时, the number of matches is counted
If you want to specify the number of substitutions, you can enter the parameters of Count
Import rea = Re.sub (' A ', ' Z ', ' b1234abc123aaa ', count=1) b = re.subn (' A ', ' Z ', ' b1234abc123aaa ', count=1) print (a) print (b)- ----Results-----b1234zbc123aaa (' b1234zbc123aaa ', 1)
Five, regular expressions
| Meta character |
Description |
| . |
Represents any character |
| | |
Logical OR operator |
| [ ] |
Match any of the inner characters or sub-expressions |
| [^] |
For character sets and fetching non- |
| - |
Define an interval |
| \ |
To the next word Fu Yingfi (usually ordinary to special, special to ordinary) |
| * |
Match previous characters or sub-expressions 0 or more times |
| *? |
Lazy Match Previous |
| + |
Matches the previous character or subexpression one or more times |
| +? |
Lazy Match Previous |
| ? |
Match the previous character or subexpression 0 or 1 repetitions |
| N |
Match the previous character or sub-expression |
| {M,n} |
Matches the previous character or subexpression at least m times up to N times |
| {N,} |
Matches the previous character or sub-expression at least n times |
| {N,}? |
Lazy Match of the previous one |
| ^ |
Matches the beginning of a string |
| \a |
Match string start |
| $ |
Match string End |
| [\b] |
BACKSPACE characters |
| \c |
Match a control character |
| \d |
Match any number |
| \d |
Match a character other than a number |
| \ t |
Match tabs |
| \w |
Match any digit letter underline |
| \w |
Do not match digit letter underline |
python-Regular basis