Re is a regular expression module for Python, which records the use of the actual development process.
The best practice for regular expressions is to use the Re.compile method after compilation, which is more efficient
1, Re.search ()
Example
TS 1423031065.017865025 2015-02-04 14:24:25 14:24:25 up, 23:51, 2 users, load average:0.02, 0.03, 0.10
Match TS begins with a space followed by several numbers.
>>> import re>>> str= "TS 1423031065.017865025 2015-02-04 14:24:25 14:24:25 up + +, 23:51, 2 users, Load average:0.02, 0.03, 0.10 ">>> import re>>> prog = Re.compile (R ' ^ts \d+ ') >>> m = Prog.sea RCH (str) >>> m.group (0) ' TS 1423031065 '
2, Re.split ()
Splitting strings is a common requirement, usually using the split method, but specifying multiple delimiters at a time is not supported by the split method, only with RE
The Split method.
Example
TS 1423031065.017865025 2015-02-04 14:24:25 14:24:25 up, 23:51, 2 users, load average:0.02, 0.03, 0.10
Split the above string with a space and.
>>> str= "TS 1423031065.017865025 2015-02-04 14:24:25 14:24:25 up +, 23:51, 2 users, load average:0.02, 0.03, 0.10 ">>> Re.split (R ' \s+|\. ', str) [' TS ', ' 1423031065 ', ' 017865025 ', ' 2015-02-04 ', ' 14:24:25 ', ' 14:24:25 ' , ' Up ', ' the ' 23:51 ', ' Days, ', ' the ', ', ' 2 ', ' Users, ', ' load ', ' average: ', ' 0 ', ' 02, ', ' 0 ', ' 03, ', ' 0 ', ' 10 ']
This article is from the "Candle Shadow Red" blog, be sure to keep this source http://gccmx.blog.51cto.com/479381/1611613
The Python regular expression re module uses