Python Learning Note 8: Regular expressions for the logo library

Source: Internet
Author: User
Tags repetition

Python has a strong standard library. From now on, start learning some of the common features available in the standard library.
First look at the regular expression (regular expressions), whose main function is to search for what you want to find by using a specific pattern (pattern) from a string.
For example: To find all the numbers from a string, we can do this:
Import restr = "Int2str" m = Re.search ("[0-9]", str) print (M.group (0))
Output: 2
Re.search () receives two parameters, the first regular expression, which is the match condition.
Re.search () If the second argument is a target string.
Returns an object m, viewing the results of a search by M.group (), or none if not found.

Common functions:
m = Re.search (pattern, string) searches the entire string until a conforming substring is found.
m = Re.match (pattern, String) checks from the beginning to see if a string conforms to a regular expression. Must match from the beginning of the first character of a string.
For the returned m, we use M.group () to invoke the result.

str = re.sub (pattern, replacement, string) is searched using the regular transform pattern in string and replaced with another string replacement for the searched string.
Returns the replaced string str.

Re.split () splits a string according to a regular expression, placing all of the split substrings in a table (list) and returning
Re.findall () searches for a string based on a regular expression, placing all the conforming substrings in a given table (list) and returning


Regular Expressions:
1) Single character:


. Any one character


A|b character A or character B


[AFG] A or F or a character of G


[0-4] 0-4 in the range of a character


[A-f] A character in the A-f range


[^m] is not a character of M


s a space


S a non-whitespace


d [0-9]


D [^0-9]


w [0-9a-za-z]


W [^0-9a-za-z]


2) Repeat


followed by a single character, representing several such similar characters


* Repeat >=0 times


+ Repeat >=1 times


? Repeat 0 or 1 times.


{m} repeats m times. For example, a{4} is equivalent to AAAA, then for example [1-3]{2} equivalent to [1-3][1-3]


{m, n} repeats M to n times. For example, a{2, 5} means a repeats 2 to 5 times. A repetition less than m, or a repetition greater than N, does not meet the criteria.
For example:
[0-9] {3,5} 9678


A?b b


A+b Aaaaab


3) Location


^ Starting position of the string


$ end position of the string


For example:
Regular expression Examples of matching strings Non-matching string


^ab.*c$ Abeec Cabeec


4) Return control


It is possible to further refine the results of the search. For example, one of the following regex expressions:
Output_ (D{4})


The regular expression encloses a small regular expression with parentheses (), d{4}. This small regular expression is used to filter the desired information from the results (here is the four-digit number).
This is part of a regular expression that is enclosed in parentheses, called a group.
We can query the group by M.group (number). Group (0) is the entire regular expression of the search results, group (1) is the first group ...
Import rem = Re.search ("Output_ ([0-9]{4})", "Output_1986.txt") print (M.group (0), M.group (1))
Output:
(' output_1986 ', ' 1986 ')


We can also use (? P<name&gt, ...) Name the group to better use the M.group query:
Import rem = Re.search ("Output_ (? P<YEAR>[0-9]{4}) "," Output_1986.txt ") Print (M.group (" Year "))

Output:1986


Insist on learning something new every day, well, I'll be here today.

Python Learning Note 8: Regular expressions for the logo library

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.