Basic Python Learning (13)

Source: Internet
Author: User

the RE module contains a pair of regular expressions. The main features and regular expressions of the RE module are described in this chapter.

What is a regular expression

Regular expressions are patterns that can match text fragments. The simplest regular expression is an ordinary string that can match itself. In other words, the regular expression ' python ' can match the string ' python '. You can use this matching behavior to search for patterns in the text, and use the computed values to concurrency a particular pattern, or to fragment the text.

* * wildcard characters

Regular expressions can match more than one string, and you can use some special characters to create such patterns. such as the dot number (. ) can match any character. When we search with window, we use a question mark (? Matches any one character, the function is the same. Then this type of symbol is called a wildcard character.

* * escape of special characters

By the above method, if we want to match "", Can we use ' ' directly? This can be done, but it will also match "pythonzorg", which is not the desired result.

All right! We need to escape it, and we can precede it with a slash. Therefore, "python\\.org" can be used in this example, so that only "" is matched.

* * Character Set

We can use the brackets ([]) to enclose the string to create a character set. You can use a range, such as ' [A-z]' to match any character from A to Z , and you can combine the range in one way or another, such as '[a-za-z0-9]'. Ability to match any uppercase and lowercase letters and numbers.

Inverse character set, you can use the ^ character at the beginning , such as '[^abc]' can match any character except a,b,C .

* * Selector character

Sometimes just want to match the string ' Python ' and ' Perl ', you can use the special characters of the selection: pipe symbol (| ) 。 Therefore, the desired pattern can be written as ' Python|perl '.

* * sub-mode

However, there are times when you do not need to use a selector for the entire pattern --- just part of the pattern. In this case, you can use parentheses to make the desired part, or sub-pattern. The precedent can be written as ' P (Ython | erl) '

* * options available

When you add a question mark to a sub-pattern, it becomes an option. It may appear in a matching string, but is not required.

R ' (heep://)? (www\.)? Python\.org '

Only the following characters can be matched:

' Http:// '

' Http:// '

' '

' '

* * Repeating sub-mode

(pattern) *: allow mode to repeat 0 or more times

(pattern) +: allow mode to repeat 1 or more times

(pattern) {m,n}: allow mode to repeat m~ n times

For example:

R ' W * \.python\.org ' matches ' ', '. ', ' '

R ' W + \.python\.org ' matches ' '; but cannot match '. '

R ' W {3,4}\.python\.org ' can only match ' ' and ' '

the contents of the RE module

some important functions in the RE module are:

Re.compile converts a regular expression to a pattern object, which allows for a more efficient match. will look for the first substring in the given string that matches the regular table. The function is found to return Matchobject (the value is True), otherwise None is returned ( the value is False) . Because of the nature of the return value, the function can be used in a conditional statement:

If Re.serch (Pat, String):

print ' Found it! '

Re.math matches the regular expression at the beginning of the given string. Therefore,re.math (' P ', ' Python ') returns True, andre.math (' P ', ' Www.python ') returns FALSE.

Re.split splits the string according to the pattern's match.

Import'>>> re.split ('[,]+', some_text) ['] 

Re. FindAll Returns all occurrences of a given pattern in the form of a list. For example, to find all the words in a string, you can do this like this:

>>>ImportRe>>> Pat =‘[a-za-z]+' >>> text =‘"Hm ... Err-is you sure? "he said, sounding insecure.' >>>Re.findall (Pat,text) [‘Hm' , 'err', 'is', ' you', 'sure', 'he', ' said', 'sounding', 'insecure ']        

the function of re.sub is to replace the substring (leftmost and overlapping substrings) of the matching pattern with the given replacement.

Import'{name}'Dear {name} ... 'Mr Gumby', text)'Dear Mr Gumby ... '          

The Re.escape function can be used to escape all characters in a string that may be interpreted as regular operators.

If the string is long and contains many special characters, and you do not want to enter a large number of backslashes, you can use this function:

>>> re.escape ('')'www\\.python\\.org' >>> re.escape ( ' But where is theambiguity?' )'but\\ where\\ is\\ the\\ ambiguity\\?'           

Matching objects and Groups

In a nutshell, a group is a submodule that is placed inside parentheses, and the number of groups depends on its left bracket. Group 0 is the entire module, so in the following mode:

' There (is a (wee) (Cooper)) who (lived in Fyfe) '

The Include groups are:

0 There is a wee Cooper who lived in Fyfe

1 was a wee Cooper

2 Wee

3 Cooper

4 lived in Fyfe

an important method for re matching objects

Here's a look at the example:

>>>Importre>>> m = Re.match (r‘www\. (.*)\.. {3}  ",  ' ' ) >>> ()  ' ">>>< Span style= "COLOR: #000000" > (0)  '< Span style= "COLOR: #800000" > ">>> (1python ">>> M.start (1 ) 4>>> M.end (1) (4, ten)            

The group method returns a string that matches the given group in the pattern, and if there is no group number, the default is 0, as above: () (0), or a single string if given a group number.

The start method returns the start index of a given group match.

The end method returns the ending index of the given group match plus 1;

Span Returns the index ofthestart and end position of the group as a tuple (start, end).


Regular expressions should be a knowledge point that is not easy to understand, and Python's boring foundation is finally finished. Although the study is not solid, but generally have an impression; the back will be very interesting, reading files, writing graphics windows, connecting databases, Web programming ....

Basic Python Learning (13)

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: 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.