Python basic learning notes (13)

Source: Internet
Author: User

 

 

ReThe module contains regular expressions. This chapter willReThe main features and regular expressions of the module are described.

 

What is a regular expression?

A regular expression is a pattern that can match a text clip. The simplest regular expression is a normal string that can match itself. In other words, regular expressions'Python'Matching string'Python'. You can use this matching behavior to search for the mode in the text, and use a value concurrency-specific mode after calculation, or segment the text.

 

**Wildcard

Regular Expressions can match more than one string. You can use some special characters to create this pattern. For example (.) Can match any character. When we useWindowQuestion mark (?) is used for search (?) Match any character for the same purpose. Such symbols are called wildcards.

 

**Escape special characters

Through the above method, if we want to match"Python.org", Directly use'Python.org'OK? This can be done, but it will also match"Pythonzorg", This is not the expected result.

Okay! We need to escape it. We can add a forward slash to it. Therefore, you can usePython \. org", Then only matching"Python.org.

 

**Character Set

we can use brackets ( [] ) enclose strings to create character sets. Applicable range, for example, ' [A-Z] 'matching A to Z can also be used together by means of one-to-one, for example, ' [a-zA-Z0-9] 'can match any size Write letters and numbers.

Reverse character set, which can be used at the beginning^Characters, such'[^ ABC]'Can match anyA,B,C.

 

**Selector

Sometimes you only want to match strings'Python'And'Perl', You can use the special character of the selected item: Pipeline symbol (|). Therefore, the required mode can be written'Python | Perl'.

 

**Sub-Mode

However, you do not need to use the selector for the entire mode.---It is only part of the pattern. In this case, you can use parentheses to start the desired part or the sub-mode. Tokens can be written'P (ython | ERL)'

 

**Optional

After a question mark is added to the sub-mode, it becomes optional. It may appear in a matching string, but it is not required.

R'(HEEP ://)? (Www \.)? Python \. org'

Only the following characters can be matched:

'Http://www.python.org'

'Http://python.org'

'Www.python.org'

'Python.org'

 

**Replay Mode

(Pattern )*:Repeated allowed Modes0Once or multiple times

(Pattern) +:Repeated allowed Modes1Once or multiple times

(Pattern) {m, n }:Repeated allowed ModesM ~ NTimes

For example:

R'W * \. Python \. org'Match'Www.python.org','.Python.org','Wwwwwww.python.org'

R'W + \. Python \. org'Match'W.python.org'; But cannot match'.Python.org' 

R'W {3, 4} \. Python \. org'Only matching'Www.python.org'And'Wwww.python.org' 

 

ReModule Content

 

ReSome important functions in the module:

Re. CompileConverting a regular expression to a pattern object can achieve more efficient matching.

Re. SearchSearches for the first substring matching the regular expression in the given string. Find function returnMatchobject (The value isTrue)Otherwise, returnNone (The value isFalse). Because of the nature of the return value, this function can be used in conditional statements:

If Re. serch (Pat, string ):

Print 'found it! '

 

Re. MathMatches the regular expression at the beginning of a given string. Therefore,Re. Math ('P','Python')Returns true,Re. Math ('P','Www. Python')False is returned.

Re. SplitSplits the string according to the match of the pattern.

>>>ImportRe>>> Some_text ='Alpha, beta, Gamma Delta'>>> Re. Split ('[,] +', Some_text )['Alpha','Beta','Gamma Delta']

 

Re. findallReturns all matching items in the given mode in the form of a list. For example, to search for all words in a string, do the following:

>>> Import  Re >>> PAT = '  [A-Za-Z] +  ' >>> Text = '  "Hm... err -- are you sure? "He said, sounding insecure.  ' >>> Re. findall (Pat, text )[  '  Hm  ' ,'  Err  ' , '  Are  ' , '  You  ' , '  Sure  ' , '  He  ' , '  Said ' , '  Sounding  ' , '  Insecure  ' ]

 

Re. subThe function is to replace the substring (leftmost and overlapped substring) in the matching mode with the given replacement content.

>>>ImportRe>>> PAT ='{Name}'>>> Text ='Dear {name }...'>>> Re. sub (Pat,'Mr. Gumby', Text)'Dear mr. Gumby...'

 

Re. EscapeFunction, which 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 input a large number of backslash characters, you can use this function:

>>> Re. Escape ('Www.python.org')'Www \. Python \. org'>>> Re. Escape ('But where is the ambiguity?')'But \ where \ is \ The \ ambiguity \\?'

 

 

 

Matching objects and Groups

 

In short, a group is a sub-module placed in parentheses. The serial number of A group depends on the number of parentheses on the left. Group0Is the entire module, so in the following mode:

'There (was a (WEE) (Cooper) Who (lived in Fyfe )'

Including groups:

0 There was a wee Cooper who lived in Fyfe

1 was a wee Cooper

2 wee

3 Cooper

4 lived in Fyfe

 

ReImportant methods for matching objects

See the example below:

>>> Import  Re >>> M = Re. Match (R '  Www \. (. *) \ .. {3}  ' , '  Www.python.org  '  ) >>> M. Group ()  '  Www.python.org  ' >>> M. group (0)  ' Www.python.org  ' >>> M. group (1 )  '  Python  ' >>> M. Start (1 ) 4 >>> M. End (1 ) 10 >>> M. span (1 )( 4, 10)

GroupThe string that matches the given group in the return mode. If no group number exists, the default value is0As shown above:M. Group () = M. group (0); if a group number is specified, a single string is returned.

StartMethod returns the start index of a given group match,

EndMethod returns the end index of the given group match.1;

SpanUsing tuples (Start,EndReturns the index of the group's start and end locations.

 

----------------------------

Regular expressions should not be easy to understand. Although I have not learned well, I have a general impression. What follows will be very interesting: Reading files, writing graphical windows, connecting to databases, and Web programming ....

 

 

 

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.