Python in Re (regular expression) module learning

Source: Internet
Author: User
Tags function prototype

Learn about regular expressions in Python today. On the syntax of regular expressions, there are many studies on the Internet without much explanation. This article mainly introduces the regular expression handler functions commonly used in Python.

Re.match

Re.match tries to match a pattern from the beginning of the string, such as: The following example matches the first word.

Import re
Text = "Jgood is a handsome boy, he's cool, clever, and so on ..."
m = Re.match (r "(\w+) \s", text)
If M:
Print M.group (0), ' \ n ', M.group (1)
Else
print ' not match '

Re.match's function prototype is: Re.match (pattern, string, flags)

The first parameter is a regular expression, here is "(\w+) \s", if the match succeeds, returns a match, otherwise returns a none;

The second parameter represents the string to match;

The third parameter is the Peugeot bit, which controls how regular expressions are matched, such as case sensitivity, multiline matching, and so on.

Re.search

The Re.search function finds a pattern match within the string, only to find the first match and then returns if the string does not match, or none.

Import re
Text = "Jgood is a handsome boy, he's cool, clever, and so on ..."
m = Re.search (R ' \shan (ds) ome\s ', text)
If M:
Print M.group (0), M.group (1)
Else
print ' Not search '

Re.search's function prototype is: Re.search (pattern, string, flags)

Each parameter has the same meaning as Re.match.

the difference between Re.match and Re.search:Re.match matches only the beginning of the string, if the string starts not conforming to the regular expression, the match fails, the function returns none, and the Re.search matches the entire string until a match is found.

Re.sub

The re.sub is used to replace a match in a string. The following example replaces a space in a string with a '-':

Import re
Text = "Jgood is a handsome boy, he's cool, clever, and so on ..."

Re.sub's function prototype is: re.sub (Pattern, REPL, string, count)

Where the second function is the replaced string, in this case '-'

The fourth parameter refers to the number of replacements. The default is 0, which means that each match is replaced.

Re.sub also allows for complex processing of replacements for matches using functions. such as: Re.sub (R ' \s ', Lambda m: ' [' + m.group (0) + '] ', text, 0); Replace the space in the string ' ' with ' [] '.

Re.split

You can use Re.split to split a string, such as: Re.split (R ' \s+ ', text), and divide the string into a word list by space.

Re.findall

Re.findall can get all the matching strings in the string. such as: Re.findall (R ' \w*oo\w* ', text); Gets all the words in the string that contain ' oo '.

Re.compile

You can compile a regular expression into a regular expression object. It is possible to compile regular expressions that are often used as regular expression objects, which can improve some efficiency. Here is an example of a regular expression object:

Import re
Text = "Jgood is a handsome boy, he's cool, clever, and so on ..."
Regex = Re.compile (R ' \w*oo\w* ')
Print Regex.findall (text) #查找所有包含 ' oo ' word
Print regex.sub (lambda m: ' [' + m.group (0) + '] ', text) #将字符串中含有 ' oo ' words are enclosed in [].
Import re
Text = "Jgood is a handsome boy, he's cool, clever, and so on ..."
Regex = Re.compile (R ' \w*oo\w* ')
Print Regex.findall (text) #查找所有包含 ' oo ' word
Print regex.sub (lambda m: ' [' + m.group (0) + '] ', text) #将字符串中含有 ' oo ' words are enclosed in [].

Http://www.cnblogs.com/sevenyuan/archive/2010/12/06/1898075.html

Python in Re (regular expression) module learning

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.