Python Regular Expression Extract string

Source: Internet
Author: User

The need to extract specific position strings in text is often met in daily work. Python's regular performance is good, it is suitable for this kind of string extraction, here to talk about the extraction technique, the basic knowledge of regular expression is not said, interested can see the RE tutorial. Extraction is generally divided into two cases, one is to extract the text in a single location of the string, and the other is to extract a number of consecutive positions of the string. This is the case with log analysis. I will tell you the corresponding method:

1. String extraction from a single location

In this case, we can use (.+?) this regular expression to extract. For example, a string "a123b" , if we want to extract the value between AB 123, you can use findall a mate regular expression, which will return a list containing so the matching situation, the code is as follows:

Import restr = "a123b" Print Re.findall (R "a (. +?) B ", str) #输出 [' 123 ']

1.1 Greedy and non-greedy matches

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 use it ? to control the case of regular greedy and non-greedy matches. The code is as follows:

Import restr = "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 print Re.findall (R" a (. +) b ", str) #输出 [' 123b456 ']print Re.findall (r" a (. *) b ", str) #输出 [' 123b456 ']

1.2 Multi-line matching

If you want multiple lines of matching, then you need to add re.S and re.M sign. When added, the newline character is matched and the re.S . . line break is not matched by default. The code is as follows:

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

re.Monce added, 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

In this case we can use (?P<name>…) this regular expression to extract. For example, if we have a line of webserver access logs: ‘192.168.0.1 25/Oct/2012:14:46:34 "GET /api HTTP/1.1" 200 44 "http://abc.com/search" "Mozilla/5.0"‘ We want to extract all the contents of this line of log, 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 fetch position. The code is as follows:

Import reline = ' 192.168.0.1 25/oct/2012:14:46:34 ' Get/api http/1.1 ' "Http://abc.com/search", "mozilla/5.0" ' 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 linebitsfor K, V in Linebits.items ():    print K + ":" +v

The result of the output is:

Status:200referrer:  Request:get/api http/1.1user_agent:mozilla/5.0date:25/oct/2012:14:46:34size:44remote_ip : 192.168.0.1

Original address: http://ju.outofmemory.cn/entry/71121

Python Regular Expression Extract string

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.