Python crawler regular expressions commonly used symbols and methods _ regular expressions

Source: Internet
Author: User

Regular expressions are not part of Python. Regular expressions are powerful tools for handling strings, with their own unique syntax and an independent processing engine that may not be as efficient as the STR-band approach, but powerful. Thanks to this, in a language that provides regular expressions, the syntax of regular expressions is the same, except that the number of grammars supported by different programming languages is different, but don't worry, unsupported syntax is usually a less common part.

1. Common symbols

. : matches any character, except the newline character \ n

: matches the previous character 0 times or infinitely
? : matches the previous character 0 or 1 times

. *: Greedy algorithm, match as many characters as possible

.*? : Non-greedy algorithm

(): The data in parentheses is returned as a result

2. Common methods

FindAll: Matches all the regular content, returns the list containing the result

Search: Matches and extracts the first conforming content, returning a regular expression object

Sub: Replaces the regular content, returns the replacement value

3, using the example

(1). Use for example, match any character, except for line break \ n

Import re #导入re库文件

A = ' xy123 '

b = Re.findall (' x.. ', a)

Print B

The results printed are: [' xy1 '], each. Represents a placeholder

(2) * Use examples, matching the previous character 0 times or infinite times

A = ' xyxy123 '

b = Re.findall (' x* ', a)

Print B

The result of printing is: [' x ', ', ', ' X ', ', ', ', ', ', ', ', ', ', ']

(3)? Use examples to match the previous character 0 or 1 times

A = ' xy123 '

b = Re.findall (' x? ', a)

Print B

The result of printing is: [' x ', ', ', ', ', ', ', ', ', ']

(4). * Examples of Use

Secret_code = ' HADKFALIFEXXIXXFASDJIFJA134XXLOVEXX23345SDFXXYOUXX8DFSE '

b = Re.findall (' xx.*xx ', Secret_code)

Print B

The results printed are: [' xxixxfasdjifja134xxlovexx23345sdfxxyouxx ']

(5). *? Examples of the use of

Secret_code = ' HADKFALIFEXXIXXFASDJIFJA134XXLOVEXX23345SDFXXYOUXX8DFSE '

c = Re.findall (' xx.*?xx ', Secret_code)

Print C

The results printed are: [' Xxixx ', ' Xxlovexx ', ' xxyouxx ']

(6) Examples of the use of ()

Secret_code = ' HADKFALIFEXXIXXFASDJIFJA134XXLOVEXX23345SDFXXYOUXX8DFSE '

D = Re.findall (' xx (. *?) xx ', Secret_code)

Print D

The result printed is: [' I ', ' love ', ' You '], the data in parentheses as the result of the return

(7) Re. Examples of the use of S

s = ' ' Sdfxxhello

Xxfsdfxxworldxxasdf ' "

D = Re.findall (' xx (. *?) xx ', S,re. S

Print D

The results printed are: [' hello\n ', ' World '], re. The role of S is to make. Include \ n when matching

(8) Examples of the use of FindAll

S2 = ' ASDFXXIXX123XXLOVEXXDFD '

F2 = Re.findall (' xx (.?) Xx123xx (.?) xx ', S2)

Print F20

The result of printing is: Love

At this point, F2 is a list that contains a tuple containing two elements, two elements of which two () matched to the content, if S2 contains more than one ' xx ' (.?) Xx123xx (.?) xx ' such substring, then F2 contains multiple tuples;

(9) Examples of the use of search

S2 = ' ASDFXXIXX123XXLOVEXXDFD '

f = Re.search (' xx (.?) Xx123xx (.?) xx ', S2). Group (2)

Print F

The result of printing is: Love

. Group (2) returns the content that the second brace matches to, and if it is. Group (1), the print is: I

(a) Sub use examples

s = ' 123rrrrr123 '

Output = Re.sub (' 123 (. *?) 123 ', ' 123%d123 '%789,s)

Print output

The results printed are: 123789123

%d of these are similar to%d in the C language, if Output=re.sub (' 123 (. *?) 123 ', ' 123789123 ', s), the output is also: 123789123

(one) \d use examples for matching numbers

A = ' Asdfasf1234567fasd555fas '

b = Re.findall (' (\d+) ', a)

Print B

The result of printing is: [' 1234567 ', ' 555 '], \d+ can match the numeric string;

These are some of the common symbols and grammars of the Python crawler's regular expressions and hope to help Python beginners learn.

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