Python RE Module small example

Source: Internet
Author: User

Today in the forum to see a question on the note down, respectively, with SED and python to write the answer, when it is the re module of the small exercise bar

2014-11-05 09:39:48:ip[192.168.10.152] logid[281424076]
2014-11-05 09:58:33:ip[192.168.10.152] logid[393876725]
2014-11-05 09:58:33:ip[192.168.10.152] logid[393883438]
2014-11-05 09:58:34:ip[192.168.10.152] logid[394052520]
2014-11-05 09:58:34:ip[192.168.10.152] logid[394057519]

the 2nd column takes the last colon, and the 3rd and 4th columns the contents of the square brackets.


Sed:

Sed ' s/://;s/ip\[\ (. *\) \]/\1/;s/\].*\[//' Test

Python:

#!/usr/bin/env pythonimport refile = open (' Test ') done = 0while not  Done:        line = file.readline ()          if line !=  ':                 line = line.strip (' \ n ')                  this_line_list = line.split ('   ')                 zz1  = re.compile (' [0-9]{2}:[0-9]{2}:[0-9]{2} ')                  zz2 = re.compile (' [0-9]{1,3}.[ 0-9]{1,3}. [0-9] {1,3}. [0-9] {1,3} ')                 zz3  = re.compile (' [0-9]{1,} ') &NBsp;               pp1 =  Re.findall (zz1,this_line_list[1])                  pp2 = re.findall (zz2,this_line_list[2])                  pp3 = re.findall (Zz3,this_line_list[3])                 print  '%s  %s %s %s '  %  (this_line_list[0],pp1[0],pp2[0],pp3[0])          else:                 done = 1file.close ()


This article is from the "Eight Miles" blog, so be sure to keep this source http://5921271.blog.51cto.com/5911271/1574944

Python RE Module small example

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.