Extracting a string with a python regular expression

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"a123b"print re.findall (r"a (. +?) b", str)# Output ['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:

ImportRestr="a123b456b"PrintRe.findall (R"a (. +?) b", str)#output [' 123 ']#? Control matches only 0 or 1, so only the match between the output and the nearest BPrintRe.findall (R"a (. +) b", str)#output [' 123b456 ']PrintRe.findall (R"a (. *) b", str)#output [' 123b456 ']

1.2 Multi-line matching

If you want to match multiple lines, you need to add re. S and re. M flag. Plus re. S ,   . Will match the newline character, the default . Does not match the line break. The code is as follows:

" a23b\na34b " Re.findall (R"A (\d+) b.+a (\d+) b", str) # Output [] # because you can't handle the case of a newline in the middle of str. Re.findall (R"A (\d+) b.+a (\d+) b", str, re. s)#s output [(' + ', ' + ')]

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

" a23b\na34b " Re.findall (R"^a (\d+) b", str) # output ['% '] Re.findall (R"^a (\d+) b", str, re. M)# output ['% ', ' "]

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:

Importreline='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 ()Printlinebits forKvinchLinebits.items ():PrintK +": "+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

Extracting a string with a python regular expression

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.