Python's regular extraction techniques __python

Source: Internet
Author: User

Turn from: http://ju.outofmemory.cn/entry/71121

method of extracting strings using Python

You often encounter the need to extract strings in text for a particular location in your daily work. Python's regular performance is good, very suitable for this kind of string extraction, here to explain the extraction of skills, regular expression of the basic knowledge is not said, interested can see the RE tutorial. Extraction is generally divided into two situations, One is to extract the string that extracts a single position in the text, and the other is to extract a string of consecutive positions. This is the case for log analysis. Below I will explain the corresponding method separately: 1. String extraction at a single location

In this case we can use (. +?) This regular expression is extracted. For example, a string "a123b", if we want to extract a value of 123 between AB, you can use FindAll to match the regular expression, which returns a list that contains the following code:

import re
str = "a123b"
print Re.findall (r "a (. +?) B ", str)
#输出 [' 123 ']
1.1 greed and non-greedy match

If we have a string "a123b456b", if we want to match all the values between A and the last B instead of the value between a and the first occurrence of B, you can control both the positive and the non greedy matches. The code is as follows:

import re
str = "a123b456b"
print Re.findall (r "a (. +?) B ", str"
#输出 [' 123 ']
#? control matches only 0 or 1, so only the match between the output and the nearest B is printed
Re.findall (r "a (. +) b", str)
#输出 [' 123b456 ' ]
print Re.findall (r "a (. *) b", str)
#输出 [' 123b456 ']
1.2 Multiple line matching

If you want to match multiple lines, then you need to add re. S and RE.M signs. After adding RE.S, the. Line breaks will be matched, default. Line breaks are not matched. The code is as follows:

str = "a23b\na34b"
Re.findall (R "A (\d+) b.+a (\d+) b", str)
#输出 []
#因为不能处理str中间有 \ n Line Change
re.findall ( R "A (\d+) b.+a (\d+) b", str, re. S)
#s输出 [(' 23 ', ' 34 ')]

Plus re. After M, the ^$ flag will match each row, and the default ^ and $ will only match the first row. The code is as follows:

str = "a23b\na34b"
Re.findall (r "^a (\d+) b", str)
#输出 [']
Re.findall (r "^a (\d+) b", str, re. M)
#输出 [' 23 ', ' 34 ']
2. String extraction from multiple locations consecutively

This situation we can use (? P<name> ...) This regular expression is extracted. For example, if we have a row of webserver access logs: ' 192.168.0.1 25/oct/2012:14:46:34 get/api http/1.1 ' http://abc.com/ Search "" mozilla/5.0 ", we want to extract all the contents of this line of log, you can write multiple (?) P<NAME>EXPR) to extract, where name can be changed to the variable you named for the location string, and expr is changed to the regular of the extraction location. The code is as follows:

Import re line
= ' 192.168.0.1 25/oct/2012:14:46:34 ' get/api http/1.1 "Http://abc.com/search" "mozilla/5.0" C1/>reg = Re.compile (' ^ (? p<remote_ip>[^]*) (? p<date>[^]*) "(?) p<request>[^ "]*" (?) p<status>[^]*) (? p<size>[^]*) "(?) p<referrer>[^ "]*" "" (?) p<user_agent>[^ "]*")
Regmatch = Reg.match (line)
linebits = regmatch.groupdict ()
print linebits
for K, v. in Linebits.items ():
    print K + ":" +v

The results of the output are:

status:200
referrer:http://abc.com/search
request:get/api http/1.1
user_agent:mozilla/5.0
Date: 25/oct/2012:14:46:34
size:44
remote_ip:192.168.0.1

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.