Python Regular Expressions

Summary: Python Regular Expression Essay 1. What is regular expression Regular expression, also known as regular expression, regular expression, regular expression, regular expression, regular expression (English: Regular Expression, often abbreviated in the code regex, regexp or RE), is a concept in computer science.

Python Regular Expression Essay

1. What is a regular expression



Regular expression, also known as regular expression, regular expression, regular expression, regular expression, regular expression (English: Regular Expression, often abbreviated as regex in the code , regexp or RE), is a concept in computer science. Regular expressions use a single string to describe and match a series of strings that match a syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that matches a pattern.

Many programming languages ​​support string manipulation using regular expressions.

In short, a regular expression is a template that can be used to match what we need, such as email format and mobile phone number format requirements.



2. Quick Start



of Regular Expressions There are many rules for regular expressions, and there are too many things to remember for beginners. In fact, it is enough to master a few basics first, and it will be faster to learn about others after applying it a few times.



Master 4 symbols: . * + ? ()

1) . matches any character except "\n"

2) * matches the previous character 0 or infinite times3

) ? matches the previous character 0 or 1

times4 ) + matches the previous character 1 time or infinite times

5) () When we need a piece of content, surround it with parentheses, and put the unnecessary content outside the parentheses.

.* Greedy algorithm, matching the maximum satisfying condition. It is to take the largest string that can match the largest string.

.*? Non-greedy algorithm, matches the smallest satisfying condition, multiple times.

The following can be understood after being familiar with it for a period of time, and written out for future reference.

\ Escape character, make the latter character change the original meaning.

If you want to match the link

0 of the picture in this web page, the

regular expression is like this:



img src="(.*?)" class="lessonimg"

in brackets That's what we want to get.

If you want to learn Python, you can come to this group, the first is 472, the middle is 309, and the last is 261, where you can learn and communicate, and there are materials you can download.



3. Use of Python regular expressions



Master a method: findall(): Match all content that conforms to the law and return a list containing the results.



import re #Introduce regular expressions# Match the content in the title tag in the web page title = re.findall('<title>(.*?)</title>',myhtml)

The following methods are used less often and can be placed understand later.



search():

Copyright statement: The content of this article is contributed by Internet users, and this community does not own the ownership and does not assume relevant legal responsibilities. If you find any content suspected of plagiarism in this community, please send an email to: [email protected] to report and provide relevant evidence. Once verified, this community will immediately delete the allegedly infringing content.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326194060&siteId=291194637